Search code examples
c#dynamics-crm

Dynamics CRM Custom Workflow plugin - read the "start when" context?


When creating a custom workflow plugin it is possible to run the process at various "Start when" conditions. AKA:

  • Record is created
  • Record status changes
  • Record is assigned
  • Record fields changed
  • Record is deleted

Is it possible to retrieve in code what the "start when" value is? For example I have one process that runs on the "Record created", but if a record is deleted ie. "Record is deleted" I would like to run the same plugin, identify that in code and then take a different path. For instance something like:

if (context.StartWhen == "Created")
{
   //Do abc
}

if (context.StartWhen == "Deleted")

{
    //Do xyz
}

I had a look at the IExecutionContext (https://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.iexecutioncontext_properties.aspx) but nothing stands out?


Solution

  • I believe you are talking about custom workflow activity. So please don't put "plugin" in the question, it's misleading.

    For both custom workflow activity and plugin, you can have the triggering event from MessageName property of IWorkflowContext or IPluginContext.

    protected override void Execute(CodeActivityContext context)
    {
        var workflowContext = context.GetExtension<IWorkflowContext>();
        var event = workflowContext.MessageName;            
    }
    
    public void Execute(IServiceProvider serviceProvider)   
    {
        var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        var event = context.MessageName;            
    }   
    

    For custom workflow activity, another option is to add a input parameter to capture the event, e.g. a string parameter called "Action", then you can configure it when using the custom workflow in CRM, and in your code you can easily check the input parameter value.