Calling Entity Bound Actions Using Xrm.Web API With Entity Collection

Introduction


In this article, we are going to discuss calling the entity bound action which has entity collection as a parameter. We need to create this parameter from an array list, while sending entity collection we need to send customer lookup as well, so let’s see how we can do that.
 

Details


Earlier, we discussed calling out of the box actions and custom actions using web API, but today, we will be using the Xrm.WebApi.execute method. You can refer to parameter details here.
 
I have created the following sample demo action which will call through Xrm.WebApi
 
  1. if (typeof (HIMBAP) == "undefined") {  
  2.     var HIMBAP = {  
  3.         __namespace: true  
  4.     };  
  5. }  
  6. HIMBAP.EvenMainLibrary = {  
  7.     RetrieveMultiple: async function (entityName, query) {  
  8.         var result = await Xrm.WebApi.retrieveMultipleRecords(entityName, query)  
  9.             .then(  
  10.                 function success(result) {  
  11.                     return result.entities;  
  12.                 },  
  13.                 function (error) {  
  14.                     return null;  
  15.                 }  
  16.             );  
  17.         return result;  
  18.     },  
  19.     CallEventAction: async function (executionContext) {  
  20.    
  21.         var formContext = executionContext.getFormContext();  
  22.    
  23.         //check for customer  
  24.         if (formContext.getAttribute("him_customer") != null &&  
  25.             formContext.getAttribute("him_customer").getValue() != null) {  
  26.             var customerId = formContext.getAttribute("him_customer").getValue()[0].id;  
  27.    
  28.             var query = "?$select=him_name,_him_customer_value&$filter=_him_customer_value eq " + customerId;  
  29.             var results = await HIMBAP.EvenMainLibrary.RetrieveMultiple("him_event", query);  
  30.    
  31.             var parameters = {};  
  32.             var entity = {};  
  33.             entity.id = formContext.data.entity.getId().substring(1, 37);  
  34.             entity.entityType = "him_event";  
  35.             parameters.entity = entity;  
  36.    
  37.             var Events = [];  
  38.             
  39.             if (results!=null && results.length > 0) {  
  40.    
  41.                 for (var indx = 0; indx < results.length; indx++) {  
  42.                     Events.push({  
  43.                         //setup current entity id  
  44.                         "him_eventid": results[indx].him_eventid,  
  45.                         //setup current entity name  
  46.                         "@odata.type""Microsoft.Dynamics.CRM.him_event",  
  47.                         //setup loopup, make sure to use schema name of the lookup field  
  48.                         "[email protected]""/accounts(" + results[indx]._him_customer_value + ")"  
  49.    
  50.                     });  
  51.                 }  
  52.    
  53.    
  54.                 if (Events.length > 0) {  
  55.                     parameters.Events = Events;  
  56.    
  57.                     var CustomEntityBoundActionDemoRequest = {  
  58.                         entity: parameters.entity,  
  59.                         Events: parameters.Events,  
  60.    
  61.                         getMetadata: function () {  
  62.                             return {  
  63.                                 boundParameter: "entity",  
  64.                                 parameterTypes: {  
  65.                                     "entity": {  
  66.                                         "typeName""mscrm.td_event",  
  67.                                         "structuralProperty": 5  
  68.                                     },  
  69.                                     "Events": {  
  70.                                         "typeName""Collection(mscrm.crmbaseentity)",  
  71.                                         "structuralProperty": 4  
  72.                                     }  
  73.    
  74.                                 },  
  75.                                 operationType: 0,  
  76.                                 operationName: "new_CustomEntityBoundActionDemo"  
  77.                             };  
  78.                         }  
  79.                     };  
  80.    
  81.                     Xrm.WebApi.online.execute(CustomEntityBoundActionDemoRequest).then(  
  82.                         function success(result) {  
  83.                             if (result.ok) {  
  84.                                 console.write("Action call completed!");  
  85.                             }  
  86.                         },  
  87.                         function (error) {  
  88.                             console.write(error.message);  
  89.                         }  
  90.                     );  
  91.                 }  
  92.             }  
  93.    
  94.         }  
  95.     }  
  96.    
  97. };  
In the above code, we are getting all the events where we have the same customer, once we have events available we are creating Events object array and adding all the events details to this object array with event details and customer lookup. Finally, we are forming a request to call our action and passing it to Xrm.WebApi.online.execute.
 
I hope it will help someone!!

Keep learning and keep sharing!!


Similar Articles
HIMBAP
We are expert in Microsoft Power Platform.