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
- if (typeof (HIMBAP) == "undefined") {
- var HIMBAP = {
- __namespace: true
- };
- }
- HIMBAP.EvenMainLibrary = {
- RetrieveMultiple: async function (entityName, query) {
- var result = await Xrm.WebApi.retrieveMultipleRecords(entityName, query)
- .then(
- function success(result) {
- return result.entities;
- },
- function (error) {
- return null;
- }
- );
- return result;
- },
- CallEventAction: async function (executionContext) {
-
- var formContext = executionContext.getFormContext();
-
-
- if (formContext.getAttribute("him_customer") != null &&
- formContext.getAttribute("him_customer").getValue() != null) {
- var customerId = formContext.getAttribute("him_customer").getValue()[0].id;
-
- var query = "?$select=him_name,_him_customer_value&$filter=_him_customer_value eq " + customerId;
- var results = await HIMBAP.EvenMainLibrary.RetrieveMultiple("him_event", query);
-
- var parameters = {};
- var entity = {};
- entity.id = formContext.data.entity.getId().substring(1, 37);
- entity.entityType = "him_event";
- parameters.entity = entity;
-
- var Events = [];
-
- if (results!=null && results.length > 0) {
-
- for (var indx = 0; indx < results.length; indx++) {
- Events.push({
-
- "him_eventid": results[indx].him_eventid,
-
- "@odata.type": "Microsoft.Dynamics.CRM.him_event",
-
- "[email protected]": "/accounts(" + results[indx]._him_customer_value + ")"
-
- });
- }
-
-
- if (Events.length > 0) {
- parameters.Events = Events;
-
- var CustomEntityBoundActionDemoRequest = {
- entity: parameters.entity,
- Events: parameters.Events,
-
- getMetadata: function () {
- return {
- boundParameter: "entity",
- parameterTypes: {
- "entity": {
- "typeName": "mscrm.td_event",
- "structuralProperty": 5
- },
- "Events": {
- "typeName": "Collection(mscrm.crmbaseentity)",
- "structuralProperty": 4
- }
-
- },
- operationType: 0,
- operationName: "new_CustomEntityBoundActionDemo"
- };
- }
- };
-
- Xrm.WebApi.online.execute(CustomEntityBoundActionDemoRequest).then(
- function success(result) {
- if (result.ok) {
- console.write("Action call completed!");
- }
- },
- function (error) {
- console.write(error.message);
- }
- );
- }
- }
-
- }
- }
-
- };
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!!