+ Start a Discussion
Chad MoutesChad Moutes 

Apex Test Class Error: CANNOT_EXECUTE_FLOW_TRIGGER

I was trying to deploy some code this morning from sandbox to production, and I received this error on a test class that was written by someone else at my company. They copied it from somewhere online and have no idea what this error is. I am curious if someone can help me figure out what is wrong with this test class that is causing the following 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 301310000008shZ. Contact your administrator for help.: [] 
Stack Trace: Class.MyProfilePageControllerTest.testSave: line 34, column 1

Here is the full test class:
 
/**
 * An apex class that keeps updates of a portal user in sync with its corresponding contact.
   Guest users are never able to access this page.
 */
@IsTest public with sharing class MyProfilePageControllerTest {
    @IsTest(SeeAllData=true) static void testSetContactFields() {
        User u = [select title, firstname, lastname, email, phone, mobilephone, fax, street, city, state, postalcode, country
                           FROM User WHERE id =: UserInfo.getUserId()];

        Contact c = new Contact();

        MyProfilePageController.setContactFields(c, u);
        System.assertEquals(c.firstname, u.firstname, 'firstname should have been set as the firstname of the user for the contact');
        System.assertEquals(c.lastname, u.lastname, 'lastname should have been set as the lastname of the user for the contact');
    }

    @IsTest(SeeAllData=true) static void testSave() {
        // Modify the test to query for a portal user that exists in your org
        List<User> existingPortalUsers = [SELECT id, profileId, userRoleId FROM User WHERE UserRoleId <> null AND UserType='CustomerSuccess'];

        if (existingPortalUsers.isEmpty()) {
            User currentUser = [select id, title, firstname, lastname, email, phone, mobilephone, fax, street, city, state, postalcode, country
                                FROM User WHERE id =: UserInfo.getUserId()];
            MyProfilePageController controller = new MyProfilePageController();
            System.assertEquals(currentUser.Id, controller.getUser().Id, 'Did not successfully load the current user');
            System.assert(controller.getIsEdit() == false, 'isEdit should default to false');
            controller.edit();
            System.assert(controller.getIsEdit() == true);
            controller.cancel();
            System.assert(controller.getIsEdit() == false);

            Contact c = new Contact();
            c.LastName = 'TestContact';
            insert c;

            c.title = currentUser.title;
            c.firstname = currentUser.firstname;
            c.lastname = currentUser.lastname;
            c.email = currentUser.email;
            c.phone = currentUser.phone;
            c.mobilephone = currentUser.mobilephone;
            c.fax = currentUser.fax;
            c.mailingstreet = currentUser.street;
            c.mailingcity = currentUser.city;
            c.mailingstate = currentUser.state;
            c.mailingpostalcode = currentUser.postalcode;
            c.mailingcountry = currentUser.country;
            controller.save();
            System.assert(Page.ChangePassword.getUrl().equals(controller.changePassword().getUrl()));
        } else {
            User existingPortalUser = existingPortalUsers[0];
            String randFax = Math.rint(Math.random() * 1000) + '5551234';

            System.runAs(existingPortalUser) {
                MyProfilePageController controller = new MyProfilePageController();
                System.assertEquals(existingPortalUser.Id, controller.getUser().Id, 'Did not successfully load the current user');
                System.assert(controller.getIsEdit() == false, 'isEdit should default to false');
                controller.edit();
                System.assert(controller.getIsEdit() == true);

                controller.cancel();
                System.assert(controller.getIsEdit() == false);

                controller.getUser().Fax = randFax;
                controller.save();
                System.assert(controller.getIsEdit() == false);
            }

            // verify that the user and contact were updated
            existingPortalUser = [Select id, fax, Contact.Fax from User where id =: existingPortalUser.Id];
            System.assert(existingPortalUser.fax == randFax);
            System.assert(existingPortalUser.Contact.fax == randFax);
        }
    }
}

Any and all help will be greatly appreciated.
Chad MoutesChad Moutes
To add to this. When running the test in the sandbox both test methods pass, testSave and testSetContactFields. The error only appears when trying to deploy apex to our production instance.
Kelly KKelly K
Hey Chad,

I just bumped into the same issue and was researching it myself. A few things I found
1. Most of the time this error can be resolved by turning off the flow. Personally, I don't consider this a fix and with large organizations, this is often not an option.
2. The process builder is still very ornery and doesn't play well with apex - especially when it comes to handling null values.

For me, I had to review the process that throwing the error and reviewed the test class it was complaining about. The issue ended up being the process builder was using Record Type in evaluation criteria, but I wasn't setting it in the test class.

I'm not sure why the process bulider would not have had access to the default record type assigned to the running user... but all I had to do to resolve it was just specify the RecordTypeId for the test record.

Hope this helps,
Kelly
Kelly KKelly K

Oh! I just noticed this - check your email, you might have gotten an email from 'FlowApplication' providing more detal on the error.

Mine looked like this - note now the 2nd line specifies which value it's upset with.

An error occurred at element myDecision (FlowDecision).
The flow failed to access the value for myVariable_current.RecordType.Name because it hasn't been set or assigned.


This report lists the elements that the flow interview executed. The report is a beta feature.
We welcome your feedback on IdeaExchange (https://success.salesforce.com/ideaSearch).
Flow Details
Flow Name: Migrate_Description_to_Description_Client_Case
Type: Workflow
Version: 5
Status: Active
Flow Interview Details
Interview Label:
Current User: EchoSign Callback Site Guest User (00530000005npvM)
Start time: 7/8/2016 8:49 PM
Duration: 0 seconds
How the Interview Started
EchoSign Callback Site Guest User (00530000005npvM) started the flow interview.
Some of this flow's variables were set when the interview started.
myVariable_old = null
myVariable_current = 5001300000v69pqAAA
ASSIGNMENT: myVariable_waitStartTimeAssignment
{!myVariable_waitStartTimeVariable} Equals {!Flow.CurrentDateTime}
Result
{!myVariable_waitStartTimeVariable} = "7/8/2016 8:49 PM"