Skip to main content

Notifications

Dynamics 365 Skilling Forum
Unanswered

My Reference

editSubscribe (0) ShareShare
ReportReport
Posted on by 354
function callPluginFromJsUsingAction(PrimaryControl) {

            var Id = formContext.data.entity.getId().slice(1, -1);
            var parameters = {};
            var entity = {};
            entity.id = Id;
            entity.entityType = "opportunity";
            parameters.entity = entity;
           
            var ActionName = {
                entity: parameters.entity,
           
                getMetadata: function() {
                    return {
                        boundParameter: "entity",
                        parameterTypes: {
                            "entity": {
                                "typeName": "mscrm.opportunity",
                                "structuralProperty": 5
                            },
                            "InputParameter":{typeName: "mscrm.opportunity",
                                       s
                                structuralProperty: 5}
                        },
                        operationType: 0,
                        operationName: "ActionName "
                    };
                }
            };
           
            Xrm.WebApi.online.execute(ActionName ).then(
                function success(result) {
                    if (result.ok) {
                        //Success - No Return Data - Do Something
                    }
                },
                function(error) {
                    Xrm.Utility.alertDialog(error.message);
                }
            );

 }

EntityReference entityref = null;

                Entity entity= null;

                if (context.InputParameters.Contains("InputParameterName"))

                {

                    entityref = (context.InputParameters["InputParameterName"] != null) ? (EntityReference)context.InputParameters["InputParameterName"] : null;

                }

                if (entityref == null)

                {

                    context.OutputParameters["Result"] = Constants.Error;

                    context.OutputParameters["Message"] = Constants.InputParameterCanNotNull;

                    return;

                }

  • Himanshu Kamboj Profile Picture
    Himanshu Kamboj 354 on at
    RE: My Reference

    Through JS calling the Action and Plugin register on Action

    ///

    First I have created a bound Action for Opportunity Entity with Input Parameter of String Type

    Parameter Name = InputParameter

    Then this Js code

    function callActionToplugin(primaryControl){

       var formContext = primaryControl

       var actionName = "abc_ActionName";

       var Id = formContext.data.entity.getId().replace('{', '').replace('}', '');  

       //Pass the input parameters to action

       var data = {

       "InputParameter": Id

       };

       //Create the HttpRequestObject to send WEB API Request

       var req = new XMLHttpRequest();

       req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/opportunities("+Id+")/Microsoft.Dynamics.CRM.abc_ActionName", true);

       req.setRequestHeader("Accept", "application/json");

       req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

       req.setRequestHeader("OData-MaxVersion", "4.0");

       req.setRequestHeader("OData-Version", "4.0");

       req.onreadystatechange = function () {

           if (this.readyState == 4 /* complete */)

           {

               req.onreadystatechange = null;

               if (this.status == 200 || this.status == 204)

               {

               setTimeout(() => {

                   //formContext.ui.refreshRibbon(true);

                   window.top.location.reload();

               }, 100);

               }

               else

               {

                   var error = JSON.parse(this.response).error;

                   alert("Error in Action: "+error.message);

               }

           }

       };

       //Execute request passing the input parameter of the action

       req.send(JSON.stringify(data));

    }

    ///////////Then Read the Input Inside the plugin:

       if (context.PrimaryEntityName.ToLower().Equals("opportunity"))
                        {
                            trace.Trace("Update of Opportunity");
                           
                            if (context.InputParameters.Contains("InputParameterName"))
                            {                  
                               var MyInputParam = (string)context.InputParameters["InputParameterName"];
                                trace.Trace("InputParameter Entity Present: "+ MyInputParam);
                                Guid oppid = new Guid(MyInputParam);
                                trace.Trace("oppidt: " + oppid);
                               
                                Entity entityData = crmService.Retrieve("opportunity", oppid, new ColumnSet(true));
                            }
                        }

Helpful resources

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,375 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 223,308 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,140

Featured topics

Product updates

Dynamics 365 release plans