Thursday 20 April 2017

Dynamics 365: Trigger action using JS

Trigger action using JS

In many cases we may encounter a requirement where we are required to perform server side logic utilizing client side events (onchange, onsave, onload…etc) in Microsoft Dynamics CRM. We generally prefer to use actions for dealing with these kinds of operations.
So in this blog, we’ll take a detailed look at how to trigger an action and retrieve the action output parameters using script with a simple example.

EXAMPLE

Let us create a simple action which will trigger on save of the account and create a primary contact.
  1. Create a simple global action which has one input and one output parameter as below.
Actions_01
The steps performed in this action are:
  • Verifying if the target account has primary contact and contains email address.
  • Creating a new contact based on the account details.
  • Updating the primary contact details on target account.
  • Returning the contact reference of the newly created contact as output parameter.
    Just to let you know, contact reference is an auto number field in the contact entity.
  1. On save of the account record, we’re going to trigger this action by using script.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var OSM=OSM || {};
OSM.Account_OnSave=function(){
  try{
        var entityId = Xrm.Page.data.entity.getId();
        var emailAddress = Xrm.Page.getAttribute("emailaddress1") != null ? Xrm.Page.getAttribute("emailaddress1").getValue() : null;
        var primarycontact = Xrm.Page.getAttribute("primarycontactid") != null ? Xrm.Page.getAttribute("primarycontactid").getValue() : null;
        if(entityId==null || entityId==""){
           return setTimeout(function(){ OSM.Account_OnSave(); }, 2000);
        }
        else if(entityId!=null && emailAddress!=null && (primarycontact==null || primarycontact==""))
           OSM.ExecuteAction(entityId);
     }
   catch (error) {
        OSM.ErrorHandler("OSM.Account_OnSave", error);
     }
}
OSM.ExecuteAction = function(entityId) {
    try{
          var actionName = "osm_CreateAccountPrimaryContact";
          var data = {
                      "AccountID": {
                          "accountid": entityId.replace("}", "").replace("{", ""),
                          "@odata.type": "Microsoft.Dynamics.CRM.account"
                       }
                    };
          OSM.odataPostMethod(data,actionName);
       }
   catch (error) {
         OSM.ErrorHandler("OSM.CallAction", error);
      }
};
OSM.odataPostMethod = function(data,action) {
     try{
            var req = new XMLHttpRequest();
            var url =Xrm.Page.context.getClientUrl() + "/api/data/v8.0/";
            if (action != null) {
                  url = url + action;
            }
            req.open("POST", url, false);
            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() {
                  //debugger;
                  if (this.readyState == 4) {
                       req.onreadystatechange = null;
                       if (this.status == 200 || this.status == 204) {
                             var result = JSON.parse(this.response);
                             if(result!=null && result.ContactReference!=null && result.ContactReference!=""){
                                   //refreshing the page to view the primarycontact details.
                                   setTimeout(function(){ Xrm.Page.data.refresh();
                                         Xrm.Page.ui.setFormNotification("Primarycontact reference is:- "+result.ContactReference, "INFO");
                                   }, 2000);
                              }
                        } else {
                              if (this.response != "") {
                                  var error = JSON.parse(this.response).error;
                                  console.log(error);
                              }
                        }
                  }
           };
           req.send(window.JSON.stringify(data));
       }
    catch (error) {
          OSM.ErrorHandler("OSM.odataPostMethod", error);
       }
};
OSM.ErrorHandler=function(objMethodName,objError){
      alert('Method name:' + objMethodName +'  '+ 'ErrorMessage : ' + objError);
}
  1. From the action response, we’re taking the ‘Contact Reference’ (output parameter) value and displaying it on the account form as a form notification.
Actions_02

Presented By Osmosys.asia (Dynamics crm)

No comments:

Post a Comment

Voice of the Customer (VOC) – Dynamics 365 CRM

Tags:   Dynamics 365   Dynamics CRM   feedback   survey   VOC   Voice of customer Introduction To Voice of the Customer For any bus...