Calling Custom Actions Using Web API

In our Web API article, we discussed about using web API actions, today we are going to discuss about calling our custom actions using Web API. If you are new to action you can refer this KB to know about actions.

Let’s take a scenario that we want to design one action for our custom event entity to approve an event based on its budget field value, for example,  if the budget is more than 50,000 then it will be approved, otherwise not. This action will have only one output parameter, which will return true or false based on if the event is approved or not. So let’s design our action first, usingthe  following steps:

 
1. Navigate to Settings, Customizations, then Customize the system.

2. Navigate to Components, Entities, then New under solution and create a custom Event entity with two below custom attributes:
 
Name                                       Data Type
Event Approved                     Two Options
Event Budget                          Currency
 
3. Place these fields over Main form and Publish your customization.

4. Navigate to Settings, Processes, then New and use the following details and click on Ok.

 

5. Setup an output argument using the following details:

 
6. Click on Add Step drop down button and add a Check Condition step.

7. Configure this step using the following condition and click on Save and Close button:

 

8. Click on the row immediatly after Check Condition and add an update step.

9. Click on Set Properties button and set Event Approval to Yes, like below: 

 
 
10. Now click on if condition and add Default step from Add Step drop down.

11. Similarly add update steps immediate after, Otherwise set and this time set Event Approved to false using Set Properties button.

12. Now add an Assign Value step outside of if condition scope and assign Event Approved field value to output argument like the following screenshot: 
 
 

13. Finally action steps should be similar to the following screenshot, now activate action using Activate button on toolbar.

 

Now our action is ready, just make sure to note downthe  name of the action and output argument during Web API call. As we don’t have any input argument that we need to pass, we can simply pass our event id and call our custom action using its name like below. After that we can capture output argument under data collection.
  1. function Callcustomaction() {  
  2.     var Id = Xrm.Page.data.entity.getId().substring(1, 37);  
  3.     var serverURL = Xrm.Page.context.getClientUrl();  
  4.    
  5.     var req = new XMLHttpRequest();  
  6.     req.open("POST", serverURL + "/api/data/v8.0/dev_events(" + Id + ")/Microsoft.Dynamics.CRM.new_CheckforBudgetApproval"true);  
  7.     req.setRequestHeader("Accept""application/json");  
  8.     req.setRequestHeader("Content-Type""application/json; charset=utf-8");  
  9.     req.setRequestHeader("OData-MaxVersion""4.0");  
  10.     req.setRequestHeader("OData-Version""4.0");  
  11.     req.onreadystatechange = function() {  
  12.         if (this.readyState == 4 /* complete */ ) {  
  13.             req.onreadystatechange = null;  
  14.             if (this.status == 200) {  
  15.                 var data = JSON.parse(this.response);  
  16.                 if (data['Approved'] != null)  
  17.                     alert(data['Approved']);  
  18.             } else {  
  19.                 var error = JSON.parse(this.response).error;  
  20.                 alert(error.message);  
  21.             }  
  22.         }  
  23.     };  
  24.     req.send();  
  25. }  
We can use the above code in script web resource and can call Callcustomaction() method on some ribbon button or some form/or field event to test it.
 
Read more articles on Dynamic CRM

Up Next
    Ebook Download
    View all
    Learn
    View all