How do I distinguish between contexts (in workflow)?

1 minute read time.

When you are in the context of an Opportunity, then you are also in the context of a Company and Person. You can see this clearly in the screen shot below.

It is easy to test for general context in CreateScripts, Validate scripts and Tablelevel scripts because you can use:

if (CRM.GetContextInfo("company","comp_companyid"))
{
Valid = false;
ErrorStr = "We are in company context";
}

But how do you tell whether you are looking at the OpportunityList of a Person or the OpportunityList of a company. This could be a very important question if you must have different workflow for when selling to an person or an organisation.

You may want to start the workflow for a new opportunity differently depending on whether you are in the primary context of a company or the primary context of a person. A workflow can have different starting points because you can define multiple primary rules for that workflow. Primary workflow rules are displayed in the interface as 'new' buttons. You can control which new button is displayed by defining a JavaScript condition for that rule.

To tell what you primary context is you must check the Key0 value in the QueryString of the URL.

Note: Key0 is also called the Dominant Key.

In a workflow primary rule you can access this with the Values() collection:

In the image below you can see that I have set the check that mean that if I am in the primary context of the company then this rule will not display.

if (Values("key0")==1)
{
//then we are in company prime context;
}

if (Values("key0")==2)
{
//then we are in person prime context;
}