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

Would you like to use Power BI to build data visualizations around your Visual Studio Team Services user stories, test cases and test results?

A handy new feature introduced with C# 7 is Local Functions. As an example, in the LINQ query below, the select method is bit hard to read and can be simplified using a helper function.

Whether you’re a user that just wants to share a view with someone or you’re an administrator/manager that would like to create and distribute personalized views, sharing personal views is a very c

When building a Web API project, you need to keep in mind how requests to your methods will be made.