[SalesForce] CANNOT_EXECUTE_FLOW_TRIGGER Error

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_EXECUTE_FLOW_TRIGGER, The record couldn’t be saved because it failed to trigger a flow. A flow trigger failed to execute the flow with version ID 301R0000000DGbx. Flow error messages: An unhandled fault has occurred in this flow
An unhandled fault has occurred while processing the flow. Please contact your system administrator for more information. Contact your administrator for help.: []

I am receiving the above error while running a test class which uses only custom metadata and no object .
The create user method in a util class where I am creating a test user is failing .

An error occurred at element myRule_1_A1 (FlowActionCall).
The flow failed to access the value for myVariable_current.CustomObject__r.Opportunity__r.Id because it hasn't been set or assigned.

P.S . I have checked several similar posts in the Forum which says that the fields used in flow should be set in the test class. But in my case , the objects aren't even used in my test class . The process builder failing runs on a different object.

How should I start solving?

Edit : [ User creation method which is failing]

public static User createUser(String profileName, Boolean isInsert){
    Profile p;

            list<Profile> listProfile = [SELECT Id FROM profile WHERE Name = :profileName];
            if(listProfile.size() > 0) {
            p = listProfile[0];
            } else {
                     return null;
                   }

           User testUser = new User(//user fields);
             if(isInsert){
                           insert testUser;
                         }

            system.assert(true);

    return testUser;
}

Best Answer

The error is happening in a Flow that was built using Visual Workflow or Process Builder. Unfortunately by default the error reporting from Flows is crap and so you just get a generic message - it wont even tell you which Flow has encountered the error.

You have to identify roughly which Flows you think it might be, and then add error handling to the Flow itself. Error Handling within Flows/Visual Workflow/Process Builder is done via visual components, each Create/Update/Delete step has a 'fault' output from which you can connect an error handler.

See this page of Salesforce manual for details: https://help.salesforce.com/articleView?id=vpm_designer_elements_connector_fault.htm&type=5

e.g. Salesforce recommend adding a 'send email' step to every 'fault' output from every Create/Update/Delete in a flow:

As a best practice, we recommend configuring the fault connectors in your flow so that you always receive an email when a flow fails. In the email, include the current values of all your flow’s resources. The resource values can give you insight into why the flow failed.

https://help.salesforce.com/articleView?id=vpm_designer_elements_connector_fault_email.htm&type=5

Once you've done all that, you should at least then know what the actual problem is (probably a validation error or something like that) and then you can fix it.