Xrm.WebApi.online.execute (Client API reference) in MSD 365

Xrm.WebApi.online.execute(request).then(successCallback, errorCallback); can be used for CURD operations, also to execute workflow and action. In the above syntax ‘request’ will be a object which can have all information of the operation. The details of the request can be found here in more depth. Here is some details ..

Here are few examples

  • Create operation
Sdk.CreateRequest = function (entityName, payload) {
    this.etn = entityName;
    this.payload = payload;

    this.getMetadata = function () {
        return {
            boundParameter: null,
            parameterTypes: {},
            operationType: 2, // This is a CRUD operation. Use '0' for actions and '1' for functions
            operationName: "Create",
        };
    };
};

// Construct a request object from the metadata
var payload = {
    name: "Softnovation Inc."
};
var createRequest = new Sdk.CreateRequest("account", payload);

// Use the request object to execute the function
Xrm.WebApi.online.execute(createRequest).then(
    function (result) {
        if (result.ok) {
            
            // perform other operations as required;
        }
    },
    function (error) {
        alert(error.message);
        // handle error conditions
    }
);
  • Retrieve operation
Sdk.RetrieveRequest = function (entityReference, columns) {
    this.entityReference = entityReference;
    this.columns = columns;

    this.getMetadata = function () {
        return {
            boundParameter: null,
            parameterTypes: {},
            operationType: 2, // This is a CRUD operation. Use '0' for actions and '1' for functions
            operationName: "Retrieve",
        };
    };
};

// Construct request object from the metadata
var entityReference = {
    entityType: "account",
    id: "0b9b8a43-e0dd-e911-a949-000d3a11e59b"
};
var retrieveRequest = new Sdk.RetrieveRequest(entityReference, ["name"]);

// Use the request object to execute the function
Xrm.WebApi.online.execute(retrieveRequest).then(
    function (result) {
        if (result.ok) {
            alert("Status: %s %s", result.status, result.statusText);
            result.json().then(
                function (response) {
                    alert("Name: %s", response.name);
                    // perform other operations as required;
                });
        }
    },
    function (error) {
        console.log(error.message);
        // handle error conditions
    }
);
  • Execute Action : with two input parameter

Below request object contains the following parameters:

  • boundParameter – we can see from the metadata that IsBound = true. This means it is bound to an entity, not global. If it is bound, we set boundParameter to the word “entity”.
  • operationName – this is the name of the action, in our case, soft_BulkClosure
  • operationType – 0 for actions
  • parameterTypes – this are the parameters we will pass to the action. This includes:
    • enumProperties – the metadata for enum types
    • structuralProperty – 0 = Unknown, 1 = Primitive, 2 = ComplexType, 3 = EnumerationType, 4 = Collection, 5 = EntityType
function RunAction() {
 
    var formContext = executionContext.getFormContext(); // get formContext

    var Id = formContext.data.entity.getId().replace('{', '').replace('}', '');
 
    var target = {};
    target.entityType = "incident";
    target.id = Id;
 
    var fromUser = {};
    fromUser.entityType = "systemuser";
    fromUser.id = "9C9CA531-E021-E911-A9BC-000D3A1B913D";
 
    var toUser = {};
    toUser.entityType = "systemuser";
    toUser.id = "6E8B062B-E021-E911-A9BC-000D3A1B913D";
 
    var req = {};
    req.FromEmail = fromUser;
    req.ToEmail = toUser;
    req.entity = target; 
    req.getMetadata = function () {
        return {
            boundParameter: "entity",
            parameterTypes: {
                "entity": { 
                    typeName: "mscrm.incident", 
                    structuralProperty: 5 
                },
                "FromEmail": {
                    "typeName": "mscrm.systemuser",
                    "structuralProperty": 5
                },
                "ToEmail": {
                    "typeName": "mscrm.systemuser",
                    "structuralProperty": 5
                }
            },
            operationType: 0, // for actions
            operationName: "soft_BulkClosure"
        };
    };
 
    Xrm.WebApi.online.execute(req).then( 
        function (data) { 
            var e = data; 
            debugger; 
        }, 
        function (error) { 
            debugger; 
            var errMsg = error.message; 
        }
    );
}

Our action looks like this, with 2 input parameters of type EntityReference User:

Execute Workflow

function RunWorkflow(executionContext) {
    
    var Id = formContext.data.entity.getId().replace('{', '').replace('}', '');
    
    var request = {};

    //pass workflow Id and entityType as worflow
    request.entity = { id: "591FDAAD-5D3A-4938-9A8F-C6132C595403", entityType: "workflow"};

    //pass record Id of type Guid ,here my workflow is on “Incident” entity

    request.EntityId = { guid: Id };
    request.getMetadata = function () {
        return {
            boundParameter: "entity", 
            operationType: 0,

                //operation name is always “Execute Workflow” independent of workflow name
                operationName: "ExecuteWorkflow", parameterTypes: {
                    "entity": {
                        "typeName": "mscrm.workflow",
                        "structuralProperty": 5
                    },
                    "EntityId": {
                        "typeName": "Edm.Guid",
                        "structuralProperty": 1
                    }
                }
    }
    };
    Xrm.WebApi.execute(request).then(
        function (data) {
            debugger;
            //successCallback
            alert("WF executed.. !!");
           
        },
        function (error) {

            //errorCallback
        });

}

That’s it for the day. Thanks !!

More info can be found here

  1. Execute workflow from JS
  2. Execute Action

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s