How to call Action with parameter(s) using Execute

2019-07-18 13:23发布

问题:

Context

I can successfully call Actions using ExecuteWorkflowRequest where the called action has no parameters:

var request = new ExecuteWorkflowRequest 
{
    EntityId = myEntityId,
    WorkflowId = myWorkFlowId,
};
service.Execute(request);

where action is a simple workflow, with Category "Action". However I can not call Actions with parameters.

What I've tried so far:

string myParameter = "Hello";
var inputArgumentCollection = new InputArgumentCollection();
inputArgumentCollection.Arguments.Add("MyParameterName", myParameter);
var request = new ExecuteWorkflowRequest 
{
    EntityId = myEntityId,
    WorkflowId = myWorkFlowId,
    InputArguments = inputArgumentCollection
};
service.Execute(request);

The called Workflow is a Category: Action with an optional string type input parameter called "MyParameterName"

This call causes an exception saying:

This workflow cannot run because arguments provided by parent workflow does not match with the specified parameters in linked child workflow.

I've also tried... Some places recommend (with no proof) for older CRM versions using the Parameters collection of the request itself... although it seems ugly and/or wrong, I gave it a shoot, with no success:

request.Parameters.Add("MyParameter", myParameter);

returns with

Unrecognized request parameter: MyParameter

Question

How can I call my parametrized Action providing parameters via API using ExecuteWorkflowRequest?

回答1:

The ExecuteWorkflowRequest is a request that was designed for executing workflows, in an older version of Dynamics CRM not yet supporting actions. It is not possible to pass arguments to it.

Instead you need to create an action with the required parameters and execute it like this:

var request = new OrganizationRequest("new_myaction")
{
    // EntityReference to the target of the action (suggested custom parameter)
    ["Target"] = myEntityId,
    // Another custom parameter
    ["MyParameterName"] = "Hello"
};

service.Execute(request);

Here "new_myaction" is the logical name of the action.