Workflow Context Target Entity When Manually Running Workflows

Printer-friendly version

I have been doing a lot of custom workflow steps lately for MS CRM. I've been using the CRM Developer Extensions which makes it so easy to get started with a plugin or custom workflow. If you add a CRM Workflow Class template, it adds the following lines (I've omitted a few others it adds for clarity):

IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

Entity entity = (Entity)context.InputParameters["Target"];

After I build my custom workflow step and began testing, I kept getting System.ServiceModel.FaultException: The given key was not present in the dictionary.

It took a little figuring out, but I finally found the cause. I was setting up my workflow to be available "On Demand". Whenever I would run it on demand, it would fail. If it ran when a record was created or when a field change, it would work. The reason it breaks is because "wfContext.InputParameters["Target"] is null when it's run manually/on demand. When it runs because on record create or a field changes values, it is populated.

So now that the problem is identified, how do you get the ID of the record that you launched the workflow from? And, if you need it, the entity logical name? It's easy; just use:

var myId = context.PrimaryEntityId;

var entityLogicalName = context.PrimaryEntityName;

The nice thing is that those values are populated when running the workflow on demand as well as when it's triggered on a create or update, so I tend to use those values instead of looking for the context.InputParameters["Target"].


About the Author:

TopLine Strategies delivers the complete integration and development of sales, marketing and customer service technologies that enable corporate clientele to improve revenue streams and strengthen customer interactions. Our project management and consulting is designed to achieve timely delivery, 100 percent user adoption of the technologies we implement and deliver measurable returns on investments for our clients.

Comments (0)

Related Blogs

TheReact Native Open Source roadmap was announced in Q4 2018 after they decided to invest more in the React Native open source community.

October is not just about pumpkins, fall foliage, and cooler temps anymore. October 2018 also means the exciting introduction of Microsoft Dynamics 365 for Customer Engagement.

Back in 2016, Microsoft introduced its intentions to refresh its CRM and ERP strategy with Dynamics 365. At the heart of its services was the Common Data Model (CDM).