Tuesday 21 April 2020

Xrm.Navigation.openForm - Open the record with specific Business Process Flow

Hi Everyone,

Today I got some strange requirement that to open the record by setting up to specific Business Process Flow on UI when clicked on the lookup from the child form.

We need to understand multiple new things from doing this.

1) addOnLookupTagClick event - Refer this to understand more about. By this event, we can prevent the default behavior of the click event on lookup.
2) OpenForm - As we all know that we have to use Xrm.Navigation.openForm for opening the records and fortunately we can pass the ProcessId and ProcessInstanceId in the entityFormOptions. In case, if you want to open the record in a specific stage, We have pass stageID.

Here is the piece of code for the same and I have called this code on the load event.

function onLoadOfOpportunity(executionContext) {
    var formContext = executionContext.getFormContext();
    formContext.getControl('new_parentopportunity').addOnLookupTagClick(function (e) {
        e.getEventArgs().preventDefault(); // disables the default behaviour of opening the record
        // Get the lookup record 
        var lookupRecord = e.getEventArgs().getTagValue();
        // Xrm.Navigation.openForm(entityFormOptions, formParameters).then(successCallback, errorCallback);
        var entityFormOptions = {};
        entityFormOptions["entityName"] = "opportunity";
        entityFormOptions["entityId"] = lookupRecord.id;
        entityFormOptions["processId"] = "EC985A4F-E5E2-489F-A13D-4AFBECD6A769";
        entityFormOptions["processInstanceId"] = "F532CF05-8B83-EA11-A811-000D3A3E14C7";
        // Open the form.
        Xrm.Navigation.openForm(entityFormOptions).then(
            function (success) {
                console.log(success);
            },
            function (error) {
                console.log(error);
            });
    });

}

When I open the record from the HomePage grid. You see the process as "Opportunity Sales Process".

The same record if open from the lookup, you see "Second Business Process Flow"


Hope this helps.

--
Happy 365'ing
Gopinath

No comments:

Post a Comment