Category Archives: Business Process Flows

Business Process Flows: Tweak A Global Process Based on User’s Role

Published by:

I recently had a requirement for Business Process Flows – a pretty standard lead to order process. The challenge was that the client had an optional first stage they called “pre-lead,” which would only apply to users on a certain inside sales team. In other words, when the Lead was created by Steve, it was supposed to get process A; but if started by Sally, it’d get process B.

Normally you could do this with two flows controlled by user roles and prioritization. But it was important that everyone have access to both flows since both types of resources could work on a given opportunity, so that was out.

The solution was to create two flows and assign it at the time the record is created.

On the User record, create a flag indicating which process they should default to.  I called it ics_defaultleadstoprelead. You’ll also need a system field on the Lead, which I called ics_processflowset, defaulted to no.

We’re going to use Xrm.Page.data.process.setActiveProcess() for this, but the gotcha is that you can’t use it on create. So our logic is:

  • If Form Type == Create
    • Hide Business Process Flow
  • Else
    • If the type has not been set yet (ics_processflowset == no)
      • Determine which process flow should be active on the Lead based on the flag on the User.
      • Set the type.
      • Set the flag (ics_processflowset) to not change it later.
    • Show the flow.

Here’s the code. It utilizes a utility library I have.

  • ics_util.getSingleton() fires a Fetch and returns one field from one returned record. This is very useful for pulling one attribute off of a given record where I have a GUID.
  • ics_util.setFieldValue() just sets a field value.

 

var ics_lead = function () {
	
	SetDefaultLeadState = function () {
	
		if (ics_util.getFormType() === ics_util.FORM_TYPE_CREATE)
		{
			Xrm.Page.ui.process.setVisible(false);
		}
		else
		{
			Xrm.Page.ui.process.setVisible(true);
			if (ics_util.getFieldValue("ics_processflowset") === false)
			{
				var fetch = 
				'<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">'+
				'  <entity name="systemuser">'+
				'    <attribute name="systemuserid" />'+
				'    <attribute name="ics_defaultleadstoprelead" />'+
				'    <order attribute="ics_defaultleadstoprelead" descending="false" />'+
				'    <filter type="and">'+
				'      <condition attribute="systemuserid" operator="eq-userid" />'+
				'    </filter>'+
				'  </entity>'+
				'</fetch>';
				
				var tupleValue = ics_util.getSingleton(fetch,"ics_defaultleadstoprelead",false,"There has been an error determining if this should be a pre-lead. (fetch)");

				// (User entity) Default Leads to Pre-Lead? (ics_defaultleadstoprelead) - "Default to Prelead" = 0; "Default to Lead" = 1
				
				// Automatically set lead or prelead process
				if (tupleValue) 
				{
					Xrm.Page.data.process.setActiveProcess(this.PROCESS_ID_NO_PRELEAD, this.onSetActiveProcess);
				}
				else
				{
					Xrm.Page.data.process.setActiveProcess(this.PROCESS_ID_PRELEAD, this.onSetActiveProcess);
				}
				ics_util.setFieldValue("ics_processflowset",true);
			}
		}
	
	};
	
	OnSetActiveProcess = function(returnStatus){
		
		switch (returnStatus) {

		case "success":
			//alert("success");
		break;

		case "invalid":
			alert("There has been an error determining if this should be a pre-lead. (setActive Process returned invalid)");
		break;
		}

	};
	return {
		setDefaultLeadState : SetDefaultLeadState,
		onSetActiveProcess : OnSetActiveProcess,
		PROCESS_ID_PRELEAD : "15dbaafe-xxxx-xxxx-xxxx-990bcd934931",
		PROCESS_ID_NO_PRELEAD : "5db8867d-xxxx-xxxx-xxxx-67afb06a53c9" 
	};
	

}();