Tuesday, December 22, 2015

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.

When I'm trying to execute a Test class I'm getting an 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.Looking at this error message I can say it is related to Flows or Process Builder but the thing is that how we need to identify in which flow it causing an error.In this post now I'm going to explain how to produce this issue and what is work around solution for this issue.

How to Produce the Issue

I have created a flow on Opportunity object with the help of Process builder .The main theme of the flow is that it has to execute an action after 6 days of  Opportunity Stage Name as Closed Won along with 2 more conditions.


Flow Criteria

1. Opportunity StageName equal "Closed Won" AND
2. Opportunity Amount is Greater than $25,000.00 AND
3. Opportunity related Account filed Sid_Desc__c(field in Account) is equals "Test"

Process Builder Flow is successfully created and activated.Please find the below image for clear picture





Now I'm going to write a sample test class to create a new opportunity

 @isTest  
  public class TestOppInsertFlow  
  {  
   static testMethod void insertOpp()  
   {  
   Opportunity op = new Opportunity();  
   op.Name = 'TestOpp';  
   op.StageName = 'Closed Won';  
   op.Closedate = System.today()+1;  
    insert op;  
   }  
  }  
When I am executing the above test class it will fail by displaying an 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.

If you check in the debug logs you can clearly observe a flow error like The flow failed to access the value for myVariable_current.Account.SicDesc because it hasn't been set or assigned see below image.





Root Cause:


In flow we are referring the opportunity related Account filed value Sid_Desc__c but in our test class we are not passing any account value for the opportunity so it referring the Sid_Desc__c value in null account value.


Work Around Solution

1. By adding a null check for relation Ship fields in Process Builder(in our case AccountId)
 

 






































2. To pass the AccoutId value to the opportunity record in test class.See modified class below
 @isTest  
  public class TestOppInsertFlow  
  {  
   static testMethod void insertOpp()  
   {  
   Account acc = new Account();  
   acc.Name = 'tesst';  
   insert acc;  
   Opportunity op = new Opportunity();  
   op.Name = 'TestOpp';  
   op.StageName = 'Closed-Won';  
   op.Closedate = System.today()+1;  
   op.Accountid = acc.id;  
   insert op;  
   }  
  }  

3.If this issue is coming whenever your trying to perform deployment into Production and if you don't have time to fix this issue,best option is to deactivate it and perform deployment and activate it again

Note:
Whenever you encountered with this type of error you will be receiving an email from salesforce





















Enjoy....... Thanks for Visiting. Please follow us at FaceBook and twitter for more updates.

2 comments: