Search code examples
pluginsdynamics-crmdynamics-crm-2016

How to return a value from plugin to action/js?


I have a button that calls an action that triggers a plugin, I want to pass a value from my plugin and show it to the user (by popup). I defined an output parameter in my action. How do I return the value (output parameter) I retrieved in my plugin back to my js function ?


Solution

  • All you have to do is, assign the output param in plugin code and consume it in JS calling code. Read more

    I'm showing the Xrm.WebApi way, but you can use XMLHttpRequest or ajax too.

    Plugin assignment C# code:

    context.OutputParameters["Output1"] = "blah blah";
    

    Custom action calling JS code:

    var parameters = {};
    parameters.Input1 = 10;
    
    var new_MyCustomActionReq = {
        Input1: parameters.Input1,
     
        getMetadata: function () {
            return {
                boundParameter: null,
                parameterTypes: {
                    "Input1": {
                        "typeName": "Edm.Int32",
                        "structuralProperty": 1
                    },
                },
                operationType: 0,
                operationName: "new_MyCustomAction"
            };
        }
    };
    
    Xrm.WebApi.online.execute(new_MyCustomActionReq).then(
        function success(result) {
            result.json().then(
                function (response) {
                    var myOutput = response.Output1;
                    alert(myOutput);
                }
            );
        }
        ,
        function (error) {
            Xrm.Utility.alertDialog(error.message);
        }
    );