Dynamics 365 – Execute Custom Action From Javascript with Input and Output Parameters

In Dynamics 365, you can call an action directly from JavaScript with its inputs. This can be achieved using the WebAPI.

First, let’s create a new custom action in Dynamics 365. Our action will accept RecordURL, Prefix, FieldsToIgnore parameters. Then it will create a copy of the Record by querying its metadata.

  • RecordURL – Input string
  • Prefix– Input string
  • FieldsToIgnore – Input string
  • ClonedGuid – Output string

Save and activate the action.

Next, let’s create a new custom action in Visual Studio. Create a new Class Library (.NET Framework).

For the code; I will use my action that Copies/Clones records dynamically in CRM. To see that post, please click here.

It’s type in Workflow Activity but don’t worry. You only need to replace Read Parameters part with below code. The rest is exactly same.

public void CloneRecord(LocalPluginContext localContext)
{
var context = localContext.PluginExecutionContext;
#region "Read Parameters"
string clonningRecordURL = context.InputParameters["RecordURL"] as string;
if (clonningRecordURL == null || clonningRecordURL == "")
{
throw new InvalidPluginExecutionException("Invalid input parameters! Please contact with your System Administrator.");
}
string[] urlParts = clonningRecordURL.Split("?".ToArray());
string[] urlParams = urlParts[1].Split("&".ToCharArray());
string entityName = urlParams[1].Split('=')[1];
string objectId = urlParams[2].Replace("id=", "");
string prefix = context.InputParameters["Prefix"] as string;
string fieldstoIgnore = context.InputParameters["FieldsToIgnore"] as string;
#endregion
context.OutputParameters["ClonedGuid"] = CloneSelectedRecord(entityName, objectId, fieldstoIgnore, prefix);
}
view raw CloneRecord.cs hosted with ❤ by GitHub

Build the Custom Action and register it to CRM by Plugin Registration Tool.

Now you can call your Custom Action from your Javascript code with WEB API. When executing action from Javascript you need to be careful about parameter names and types. To see all typeNames and structuralProperty enum values check microsoft official docs and you can find further explanation there.

CopyOpportunity: function (primaryControl) {
try {
//showProgressIndicatior
Xrm.Utility.showProgressIndicator("Please wait while processing your request");
//Save current record first once copy button is getting clicked
primaryControl.data.save().then(function () {
var recordURL = primaryControl.getUrl();
var actionName = "ad_CloneRecord";
executeAction(recordURL, actionName, primaryControl);
},
function () {
alert("An Error has occured. Please contact with your System Administrator.");
});
} catch (error) {
Xrm.Utility.closeProgressIndicator();
console.log(error);
throw "An Error has occured. Please contact with your System Administrator.";
}
},
executeAction: function (recordURL, actionName, primaryControl) {
var req = {};
req.RecordURL = recordURL;
req.Prefix = prefix;
req.FieldsToIgnore = fieldsToIgnore;
req.getMetadata = function () {
return {
boundParameter: null,
parameterTypes: {
"RecordURL": {
typeName: "Edm.String",
structuralProperty: 1
},
"Prefix": {
typeName: "Edm.String",
structuralProperty: 1
},
"FieldsToIgnore": {
typeName: "Edm.String",
structuralProperty: 1
},
},
operationType: 0,
operationName: actionName
};
};
var successCallbackExc = function (result) {};
var errorCallbackExc = function (error) {
Xrm.Utility.closeProgressIndicator();
alert(error.message);
};
Xrm.WebApi.online.execute(req).then(successCallbackExc, errorCallbackExc);
},
Advertisement

One thought on “Dynamics 365 – Execute Custom Action From Javascript with Input and Output Parameters

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 )

Facebook photo

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

Connecting to %s