Realtime workflow and pre-Operation

Realtime workflow capability is a great addition to Dynamics CRM. It can be used as replacement to plugin, as it exhibits the same synchronous behaviour that executes as a part of the database transaction i.e. any error in the realtime workflow would rollback the transaction.

However in terms are capability, there seems to be two, that are lacking compared to a plugin.

  1. Create – Pre-Operation
  2. Delete – Post-Operation

This can be seen in the UI for realtime workflow. There are two dropdowns that are never editable, when you open a realtime workflow.

Workflow Options

Through this post, I will demonstrate how you can use a realtime workflow to run pre-operation on create and use the workflow context to modify the entity record, before it is passed to the main operation in the pipeline.

Use the below fetchxml, to identify which stage the realtime workflow will run. For create, this will default to “4o” which means it is going to run post operation.

<fetch count="50" >
  <entity name="workflow" >
    <attribute name="triggeroncreate" />
    <attribute name="name" />
    <attribute name="createstage" />
      <condition attribute="mode" operator="eq" value="1" />
      <condition attribute="category" operator="eq" value="0" />
      <condition attribute="type" operator="eq" value="1" />

You then have to update the attribute to “20”, using the CRM SDK. You can use the simple snippet below to do this.

CrmServiceClient crmSvc = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CRMConnectionString"].ConnectionString);

if (crmSvc.IsReady)
	var workflowUpdate = new Entity("workflow");
	workflowUpdate.Id = new Guid("[WORKFLOWID]");
	workflowUpdate["createstage"] = new OptionSetValue(20);

This will mean this the workflow is going to run pre-operation of the create message. Now, let see how to modify the entity that is going to be passed on to the pipeline. You would have to write a custom workflow step to do this. In this sample workflow step, I am setting the salutation to “Mr” when the gender is “Male” and to “Ms” when the gender is “Female”.

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

namespace SampleWorkflow
    public class UpdateGenderActivity : CodeActivity
        protected override void Execute(CodeActivityContext executionContext)
            ITracingService tracer = executionContext.GetExtension<ITracingService>();
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            var entity = (Entity)context.InputParameters["Target"];
            if(context.MessageName.Equals("Create", StringComparison.InvariantCultureIgnoreCase) &&
                context.PrimaryEntityName.Equals("contact", StringComparison.InvariantCultureIgnoreCase))
<optionSetValue>("gendercode")?.Value == 1)
                    entity["salutation"] = "Mr";
                else if (entity.GetAttributeValue
<optionSetValue>("gendercode")?.Value == 2)
                    entity["salutation"] = "Ms";

So, what it the difference between these two stages. Lets look at the audit trail for the workflow that ran pre-operation on create.

Create Audit

Now lets create a new realtime workflow, that does it in the default post-operation on create. Here is the workflow definition.

Workflow Definition

Here is the audit trail for this workflow, after the record has been created.

Update Audit

As you can see, that contact is first created without “Salutation” and then the realtime workflow runs to update the “Salutation” based on the gender. This is in contrast to the realtime workflow that runs on pre-operation and makes the relevant contact attribute changes, before it is passed on to the main operation.

Quicktip on update pre-operation:

When you select the realtime workflow to run pre-operation, any attributes you access from the target entity in the workflow steps are from the pre-images i.e. these are the old values. The current value is not passed to the realtime workflow. The CRM interface allows you to run the workflow pre-operation for status change, owner change and field change.



  1. Event Execution Pipeline
  2. Create realtime workflows


  1. It is undocumented, so it could be unsupported. Even after updating the workflow stage to 20, I was able to open it without any issue. However, the UI still displays the trigger stage as post-operation, even though it is pre-operation in the database.

Leave a Reply

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

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

Facebook photo

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

Connecting to %s