Unit test for Pause Reason in a Screen Flow

I’ve created a script in apex that captures the pause reason in a screen flow.
In general, I use the FlowRecordRelation and FlowInterview to capture the pause reason and save it to a specific field.

The script is working and now I need to test it.

So I started by trying to insert a flow:

@IsTest
private static void testCopyPausedReasonFromFlow() {
    
    My_Object__c q = [SELECT Id FROM My_Object__c];

    {
        Map<String, Object> inputs = new Map<String, Object>();
        inputs.put('recordId', q.Id);
        Flow.Interview.My_Object_Flow_V1 myFlow =  new Flow.Interview.My_Object_Flow_V1(inputs);
        myFlow.start(); 
    }       
     
}

but this didn’t work because myFlow.start() threw an error:

System.FlowException: Start can’t be called on a flow with the process
type Screen Flow.

I then found that the start method only works for autolaunched flow or user provisioning flow per this article.

So my question is:

Is there a way to create FlowInterview and a FlowRecordRelation records in a unit test without doing the above? The documentation is really limited and this is the actual goal here.

Answer

There is no way to create a FlowInterview regardless of the Test context for Flows other than autolaunched type.

As an example, if you had an screen flow, you could not mock the screen user interaction from you apex code, that is why it does not have much sense to be able to start them, since they will get stuck at that point.

For the good or the bad, Salesforce does not force you to write Apex Test to deploy Flows directly as Active if they are not autolaunched, because this option, even enabled, only applies to processes and autolaunched flows.

As the documentation states:

By default, active processes and flows are deployed as inactive. After deployment, you manually reactivate the new versions. If your org uses a continuous integration and continuous delivery model to deploy metadata changes, enable the option to deploy processes and flows as active. This option applies to processes and autolaunched flows that are deployed via change sets and Metadata API.

Attribution
Source : Link , Question Author : Json , Answer Author : arut

Leave a Comment