Wed, 12/15/2021 - 06:05 By Pham Ba Son Contributor Lloyd Sebag

Dataverse Execute process from javascript with entity context

A custom process (action, workflow) in Power Platform often executed on the scrope of an entity. Usually, this bound entity is passed into the execution context by default if the process is triggered by customized conditions like creation, deletion or update of chosen attributes. Another method to have this bound entity in execution context is to receive it from input arguments.

Our client api object Xrm now provides us with the ability to bind an entity as scope on calling execute of a process. This makes client side javascript a feasible choice on development cases that involves custom process.

Use WebApi action

A quick example

I have this custom action which will update the summary field of the bound contact on execution. This action has no input argument. It works only with the bound entity which is a contact.

Dataverse Execute process from javascript with entity context

Dataverse Execute process from javascript with entity context

The requirement is to execute this action from client-side javascript with a bound entity. I achieve this using the following code.

var CallSummarizeContact = (function () {
    function CallSummarizeContact(contactId) {
        this.entity = {
            'id': contactId,
            entityType: "contact"
        };
    }
    CallSummarizeContact.prototype.getMetadata = function () {
        return {
            boundParameter: "entity",
            operationName: "dev_SummarizeContact",
            operationType: 0,
            parameterTypes: {
                "entity": {
                    "typeName": "mscrm.contact",
                    "structuralProperty": 5
                }
            }
        };
    };
    return CallSummarizeContact;
}());

var summarize = function() {
    // get contact id
    const xrm = Xrm;
    const formContext = Xrm.Page;
    const contactid = formContext.entityReference.id.replace("{", "").replace("}", "");

    // call summarize contact
    var summarizeContactRequest = new CallSummarizeContact(contactid);

    Xrm.WebApi.online.execute(summarizeContactRequest).then((result) => {
        console.log(result);
    }, (error) => {
        console.log(error);
    });
}

I create an object which contains informations needed for the execution request. Here we have 1 parameter passing to the process despite of the process does not have any input argument. It is the bound entity that we need.

Dataverse Execute process from javascript with entity context

 

Dataverse Execute process from javascript with entity context

Add new comment

Image CAPTCHA
Enter the characters shown in the image.