Calling Entity bound Actions using Xrm.Web API with Entity Collection

Requirement
In this article we are going to discuss about calling entity bound action which have entity collection as parameter, we need to created this parameter from 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 about calling out of the box actions and custom actions using webapi but today we will be using Xrm.WebApi.execute method, you can refer parameter details from here.

I have created following sample demo action which will be calling through Xrm.WebApi
customaction

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();

        //check for customer
        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({
                        //setup current entity id
                        "him_eventid": results[indx].him_eventid,
                        //setup current entity name
                        "@odata.type": "Microsoft.Dynamics.CRM.him_event",
                        //setup loopup, make sure to use schema name of the lookup field
                        "him_Customer@odata.bind": "/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 above code we are getting all the events where we have 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 request to call our action and passing it to Xrm.WebApi.online.execute.

Hope it will help someone !!
Keep learning and Keep Sharing !!

Leave a Reply

Your email address will not be published. Required fields are marked *