Work Relay objects in Unit Tests

Below is an example of apex code creating Work Relay records - Flow, Flow Instance, Flow Instance Cursor - for the test purposes. This code can be used in Unit Tests.  Example is using Account as a source object for the Work Relay records.

// getFlowInstanceCursor API method inserts Flow, Flow Instance and Flow Instance Cursor records, and returns Flow Instance Cursor record. 
WR_BPM__Flow_Instance_Cursor__c cursor = (WR_BPM__Flow_Instance_Cursor__c)new WR_BPM.API().call('FlowApexTestsEnvironment', 'getFlowInstanceCursor', null);

// Flow created by getFlowInstanceCursor API method has Object Type set to Stub__c. 
// Need to delete Instance created by getFlowInstanceCursor API method because Flow's Object Type field can not be updated if there are instances on the Flow
WR_BPM__Flow_Instance__c cursorsInstance = [select id, name, WR_BPM__Object_Type__c, WR_BPM__Object_Name__c, WR_BPM__Object_Id__c, WR_BPM__Object__c from WR_BPM__Flow_Instance__c where id = :cursor.WR_BPM__Flow_Instance__c limit 1];
delete cursorsInstance;

// update Flow with correct Object Type. Object Type should be in lowercase. 
WR_BPM__Flow__c flowToUpdate = [select id, name, WR_BPM__Object_Type__c from WR_BPM__Flow__c where id = :cursor.WR_BPM__Flow_Instance__r.WR_BPM__Flow__c limit 1];
flowToUpdate.WR_BPM__Object_Type__c = 'account';
update flowToUpdate;

// create Instance that will have Object Type inherited from Flow (that's a formula field on Instance object). 
// replace {Account.Id} and {Account.Name} with custom object record data.
WR_BPM__Flow_Instance__c flowInstance = new WR_BPM__Flow_Instance__c(WR_BPM__Flow__c = flowToUpdate.Id, WR_BPM__Is_Active__c = true, WR_BPM__Object_Id__c = '{Account.Id}', WR_BPM__Object_Name__c = '{Account.Name}');
insert flowInstance;

// create new cursor referring new Instance (if it's needed)
WR_BPM__Flow_Instance_Cursor__c flowInstanceCursor = new WR_BPM__Flow_Instance_Cursor__c(WR_BPM__Flow_Instance__c = FlowInstance.Id, WR_BPM__Step__c = cursor.WR_BPM__Step__c, WR_BPM__Step_Assigned_To__c = UserInfo.getUserId(), WR_BPM__Step_Assigned_Type__c = 'User', WR_BPM__Status__c = 'In Progress', WR_BPM__Step_Changed_Date__c = Date.today());
insert FlowInstanceCursor;
Click to copy

0 Comments

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.