Calling Action from JavaScript.

 Create a action, with input parameters  First Name and Last name, Out put Full name. create a plugin on custom action call, read the inputs and prepare full name with First name and last Name. send the output to action. Call this action from Javascript, get the output and sent ""fullname "" field valuetrigger the javascript on - Onsave of your custom entity. create First name, Last Name and Full name fields in your custom entity.


Step1: For this fist need to create one action with 2 inputs and one output


Step2: After creating action we need to prepare one plugin that combining the both fist name and last name

 public void Execute(IServiceProvider serviceProvider)

        {

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            IOrganizationService service = (IOrganizationService)factory.CreateOrganizationService(context.UserId);

            ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));


            try

            {

                var Fistname = (string)context.InputParameters["FistName"];

                tracing.Trace(Fistname);

                var Lastname = (string)context.InputParameters["Lastname"];

                var Fullname = Fistname + "  " + Lastname;

                tracing.Trace(Fullname);

                context.OutputParameters["FullName"] = Fullname;


            }

            catch (Exception ex)

            {

                throw new InvalidPluginExecutionException(ex.Message);

            }

Give the same names what u given in action, so that we can read the inputs and set the output for parameter.

After Creating plugin, create a JavaScript to call the action by using XRM toolbox                                                                                                                                                                                                            function CallingActions(executionCOntext) {

    var formContext = executionCOntext.getFormContext();

// read the record guid 

    var iddd = formContext.data.entity.getId();

// read the form fist name 

    var fistnmes = formContext.getAttribute("cr245_fistname").getValue();

//read the form last name

    var Lastnmes = formContext.getAttribute("cr245_lastname").getValue();

    var parameters = {};

    var entity = {};

    entity.id = iddd;

    entity.entityType = "crc78_employ";

    parameters.entity = entity;

    parameters.FistName = fistnmes;

    parameters.Lastname = Lastnmes;


    var new_SeetingfullnameRequest = {

        entity: parameters.entity,

        FistName: parameters.FistName,

        Lastname: parameters.Lastname,


        getMetadata: function () {

            return {

                boundParameter: "entity",

                parameterTypes: {

                    "entity": {

                        "typeName": "mscrm.crc78_employ",

                        "structuralProperty": 5

                    },

                    "FistName": {

                        "typeName": "Edm.String",

                        "structuralProperty": 1

                    },

                    "Lastname": {

                        "typeName": "Edm.String",

                        "structuralProperty": 1

                    }

                },

                operationType: 0,

                operationName: "new_Seetingfullname"

            };

        }

    };


    Xrm.WebApi.online.execute(new_SeetingfullnameRequest).then(

        function success(result) {

            result.json().then(

                function (response) {


                    var datas = {

// read the output like this

                        "crc78_name": response.FullName,

                    }

                    Xrm.WebApi.online.updateRecord("crc78_employ", iddd, datas).then(

                        function success() {

                            alert("success");

                        },

                        function (error) {

                            Xrm.Utility.alertDialog(error.message);

                        }

                    );

                }

            );

        },

        function (error) {

            Xrm.Utility.alertDialog(error.message);

        }

    );

}                                                                                                                                                                   


After calling action using JavaScript, create web resource and the function on save of the form  

After that register the plugin in plugin register tool and call the action in message                                       


Comments

Popular posts from this blog

create HTML page show all contact related to specific account, add this webresource as a subgrid in the form

Connect MS Crm To Postman For Webapi Calls.

create one PCF control and embed into crm