简体   繁体   中英

How to determine the Workflow Mode in the CRM 2011?

I want to determine how the workflow is initiated (background/asynchronous) or (real-time/on-demand) in the Microsoft CRM 2011? In both of the OOB Workflow Conditions and within a Custom Workflow Activity?

Based on the following link, I can see Microsoft introduced a new property ( IWorkflowContext.WorkflowMode ) in CRM 2013 SDK to expose that. I want the same information in CRM 2011.

http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.workflow.iworkflowcontext.workflowmode(v=crm.6).aspx

I do not think that it is possible to differentiate between on-demand and triggered workflows in the workflow designer.

In a custom workflow activity there will only be the Target input parameter if the workflow was started by a trigger:

var context = activityContext.GetExtension<IWorkflowContext>();
if (context.InputParameters.Contains("Target"))
{
    // Started by a trigger
}
else
{
    // Started on demand
}

In addition to that you can determine what kind of trigger started the workflow by the type of Target. This info is also available in context.MessageName which contains the string "Update" in the case of an update message.

if (context.MessageName == "Update")
    // this is an update message
var context = activityContext.GetExtension<IWorkflowContext>();
if (context.InputParameters.Contains("Target"))
{
    if (context.InputParameters["Target"].GetType() == typeof(Entity))
    {
        // create and update are Entity
    }
    else if (context.InputParameters["Target"].GetType() == typeof(EntityReference))
    {
        // delete and some other operations are EntityReference
    }       
}
else
{
    // Started on demand
}

Which specific fields were updated can be determined by which attributes Target contains.
This could be used to determine which field triggered the workflow.
If a create message triggered the workflow then Target will contain all fields (that the user entered). To be certain that the workflow was triggered by an update of a field you could use context.MessageName in conjunction with checking which attributes Target contains.

The functionality you seek does not exist in CRM 2011. All workflows in CRM 2011 run asynchronously therefore there is no need for IWorkflowContext.WorkflowMode .

You cannot make CRM 2011 run an OOTB workflow synchronously. If you want to execute code during the transaction, the way CRM 2013 can do with synchronous workflows, you must code the logic in a Plugin.

I think you can achieve your goals by adding a “hidden” field on your entity and creating a couple “handler” workflows that populate this field then fire your “actual” workflow.

Step 1. Create a field on your entity to hold a value indicating the initiator

Step 2. Create the “actual” workflow as a child process

Step 3. Create an on-demand (only) workflow that:
a. Sets a value on the entity in the “initiator” field indicating the workflow was run manually.
b. Calls the child workflow

Step 4. Create a workflow with on-demand not selected that:
a. Sets a value on the entity in the “initiator “ field indicating the workflow was automated.
b. Calls the child workflow

Step 5. Show the result
a. Create an “admin” form that displays this value and/or,
b. Make the field hidden on the user form and show via js as appropriate and/or,
c. Create a view that includes displays this field and/or,
d. Create a report that includes this field and/or,
e. Check the value of this field in your code

Unless there is specific value, this field would not be included on user forms or views.

By using a custom field and populating it via “handler” workflows you will be able to achieve your OOB goal. (That is if you consider using the web GUI to create custom fields and workflows “OOB”.)

By checking the value of this field programmatically you will achieve your Custom Workflow goal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM