Call action with input & output parameters

Action in MSD 365

Action with input and output parameter

Accept input parameter and set output parameter in custom WF/Plugin

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;

namespace Soft.Softnovation.Xrm.Workflow
{
    /// <summary>
    /// this triggers on change of 'xyz field'
    /// </summary>
    public class OnChnageOfABCnXYZ : CodeActivity
    {
        [Input("personId")]
        public InArgument<string> personId
        {
            get; set;
        }

        [Output("ShowError")]
        public OutArgument<string> Showerror
        {
            get; set;
        }
                
        protected override void Execute(CodeActivityContext executionContext)
        {
            ITracingService tracingService = executionContext.GetExtension<ITracingService>();

            try
            {
                tracingService.Trace("Start of sample code ");

                Guid scrPrID = new Guid(this.personId.Get(executionContext));
                
                //your logic 
                //your logic 
                //your logic 
                //your logic 
                //your logic 

                Showerror.Set(executionContext, "Set your output message here");

                tracingService.Trace("End of Sample code");

            }
            catch (Exception ex)
            {
                tracingService.Trace("Exception: {0}", ex.Message);
            }
        }

    }
}

JS code which will work as mediator between C# code and MSD 365 Action is here it will be trigger and push the input parameter and accept output parameter.

function OnChangeOfNextStage(executionContext) {

    Xrm.Utility.showProgressIndicator("Processing...");

    //get the current organization name
    var serverURL = Xrm.Page.context.getClientUrl();
    //query to send the request to the global Action 
    var actionName = "soft_Your_Action_Name"; // Global Action Unique Name 
    var scrPersonId = formContext.data.entity.getId().replace('{', '').replace('}', '');
    //Pass the input parameters of action
    var data = {
        "personId": scrPersonId
    };
    //Create the HttpRequestObject to send WEB API Request
    var req = new XMLHttpRequest();
    //Post the WEB API Request  
    req.open("POST", formContext.context.getClientUrl() + "/api/data/v9.0/dhhs_screeningpersonses(" + scrPersonId + ")/Microsoft.Dynamics.CRM." + actionName, true); //use this if action is Entity specific 
    //req.open("POST", serverURL + "/api/data/v9.0/" + actionName, true); //  use this if action is global
    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) {
                //You can get the output parameter of the action with name as given below
                result = JSON.parse(this.response);
                if (result.ShowError != null && result.ShowError != "") {

                    openOOBAlertQueueItem("", result.ShowError);
                    Xrm.Utility.closeProgressIndicator();

                }

            }
            else {
                var error = JSON.parse(this.response).error;
                alert("Error in Action: " + error.message);
                Xrm.Utility.closeProgressIndicator();
            }
        }
    };
    //Execute request passing the input parameter of the action  
    req.send(window.JSON.stringify(data));
}

openOOBAlertQueueItem = function (buttonLabel, alertText, _successFunction, _failureFunction) {
    var alertStrings = { confirmButtonLabel: buttonLabel, text: alertText };
    var alertOptions = { height: 200, width: 500 };
    Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(function success(result) {
    },
        function (error) {
        });
};

That’s it for now. Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s