> For the complete documentation index, see [llms.txt](https://docs.work-relay.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.work-relay.com/automations/apex-code/work-relay-objects-in-unit-tests.md).

# Work-Relay objects in unit tests

Below is an example of Apex code creating Work Relay records—Flow, Flow Instance, and Flow Instance Cursor—for test purposes. This code can be used in Unit Tests. The example uses Account as a source object for the Work Relay records.

{% code overflow="wrap" lineNumbers="true" %}

```apex
// 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;
```

{% endcode %}
