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

If you’ve spent any time developing with the CRM mobile app, you know how frustrating it can be when trying to track down bugs.

A while back I had set up a Hyper-V virtual machine, but it had been a while since I've needed to use it again. The time came again recently as I needed a VM to do some OS-specific testing.

Have you given your independent sales reps CRM licenses so they can access CRM directly, rather than going throug

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