Calling an Dynamics 365 Action from JavaScript

Leave a comment

In this post we will call an action from JavaScript.

In a previous post, we created an action that takes 2 email inputs, a “from email” and a “to email”. When a case is created, if the case contains the word “bug”, an email is sent from the from email, and sent to the to email. The case is then assigned to the user of the “to email”.

Let’s now call this action from JavaScript. We will call this from the OnSave event of a case.

Go to the OnSave properties and click Add:

Click New:

Name the JS and click on Text Editor:

Now the code. We will need the Ids of the two users. We can look these up with a Retrieve and pass them to the action, but instead we will hardcode them and pass them. As we are using an entity reference, we will need to find the primary key name by going to the entity:

Now the JavaScript:

function RunAction() {

    var Id = Xrm.Page.data.entity.getId().replace('{', '').replace('}', '');
    var serverURL = Xrm.Page.context.getClientUrl();
    var fromUser = {};
    fromUser.systemuserid = "9C9CA531-E021-E911-A9BC-000D3A1B913D";
    var toUser = {};
    toUser.systemuserid = "6E8B062B-E021-E911-A9BC-000D3A1B913D";

    var data = {
        "FromEmail": fromUser,
        "ToEmail": toUser
    };

    var query = "incidents(" + Id + ")/Microsoft.Dynamics.CRM.new_RouteBugtoDeveloper";
    var req = new XMLHttpRequest();
    req.open("POST", serverURL + "/api/data/v9.1/" + query, true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function () {
        if (this.readyState == 4 /* complete */) {
            req.onreadystatechange = null;
            if (req.status >= 200 && req.status <= 300) {
                var data = JSON.parse(this.response);
            } else {
                var error = JSON.parse(this.response).error;
            }
        }
    };
    req.send(window.JSON.stringify(data));
}

In the code, we are creating objects for the to and from users, as these are entityreferences in our process:

We then pass the Id of the case we would like the action to run on, and add “Microsoft.Dynamics.CRM” to the action name new_RouteBugtoDeveloper before passing it to the API.

On success, there will be nothing returned in the data variable.

Possible errors

“Resource not found for the segment” – this can happen if the action name is not found, for example, if new_RouteBugtoDeveloper is misspelled. Confirm the spelling and case.

“An error occurred while validating input parameters: Microsoft.OData.ODataException: One or more parameters of the operation ‘new_RouteBugtoDeveloper’ are missing from the request payload. The missing parameters are: FromEmail.” This happens if for example a parameter is not provided, in this case the FromEmail parameter.

“An error occurred while validating input parameters: Microsoft.OData.ODataException: The parameter ‘FromEmail1’ in the request payload is not a valid parameter for the operation ‘new_RouteBugtoDeveloper’. In this case, an invalid parameter was provided, FromEmail1.

“An error occurred while validating input parameters: Microsoft.OData.ODataException: Does not support untyped value in non-open type.” In this case, one of the input parameters does not contain the primary key for the object.

 

ABOUT CARL DE SOUZA

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 *