Calling a Dynamics 365 Action from JavaScript using

Leave a comment

In this post, we will continue with our example of calling a Dynamics 365 action from JavaScript, this time using the Xrm.WebApi.

In the previous example, on saving a case, we would call an action to send an email to a developer if the title of the case contained the word “bug”. The action would accept 2 inputs, a “to” user and a “from” user to send the email to and from.

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

We will need to get the metadata for this action. To do this, browse to$metadata and find your action, in this case new_RouteBugtoDeveloper. You will see the parameters and the types:

Now, we can build the request to send to the xrm.webapi endpoint. The details of the request can be found here:

The 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, new_RouteBugtoDeveloper
  • 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

Now the code. We are creating an object for the target, which we will run our code on. We are also creating an object for the FromUser and the ToUser. We will pass all of these as parameters to our action:

function RunAction() {

    var Id ='{', '').replace('}', '');

    var target = {};
    target.entityType = "incident"; = Id;

    var fromUser = {};
    fromUser.entityType = "systemuser"; = "9C9CA531-E021-E911-A9BC-000D3A1B913D";

    var toUser = {};
    toUser.entityType = "systemuser"; = "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,
            operationName: "new_RouteBugtoDeveloper"
        function (data) { 
            var e = data; 
        function (error) { 
            var errMsg = error.message; 

Now, we will run this on saving a case. We can see the URL as:

The action has run successfully.



Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, BI, Web, Cloud and Data Science.

carldesouza.comLinkedIn Twitter | YouTube


Leave a Reply

Your email address will not be published. Required fields are marked *