function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Preeti Agarwal 29Preeti Agarwal 29 

Lightning Flow before save to update name field of opportunity, failing test classes

I am using Flow before save to update name field of opportunity and its causing my test classes fail FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]
Best Answer chosen by Preeti Agarwal 29
Danish HodaDanish Hoda
Hi Preeti,
Test class flow works the same as the data is being created from the UI i.e. page-layouts.
Please refer Order of execution (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm) for better understanding. The data created checks for the Standard Validation like - manadatory fields on page layout, field length etc. and then proceed for before save update flow and then before trigger and custom validation rules and so on..

All Answers

Danish HodaDanish Hoda
Hi Preeti,
Test class flow works the same as the data is being created from the UI i.e. page-layouts.
Please refer Order of execution (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm) for better understanding. The data created checks for the Standard Validation like - manadatory fields on page layout, field length etc. and then proceed for before save update flow and then before trigger and custom validation rules and so on..
This was selected as the best answer
Danish HodaDanish Hoda
So try inserting the Opportunity record with any test value, you will see the inserted Opportunity record will have the name as you have ogic in before-save update flow.
Preeti Agarwal 29Preeti Agarwal 29
Thanks, Danish for your quick responses, Your responses made me think and believe in salesforce, I checked order of execution and looked at the debug logs and tried to find what could have gone wrong, So I found one of the lookup fields which was used to update the name of opportunity was not passed in test data, so I wrote a condition, because in flow if no values is coming through a query its not considering is empty string.

Example: 
IF(ISBLANK({!$Record.npsp__Primary_Contact__c}),  
 'Scholar' +  ' ' + TEXT({!$Record.Reason__c}),
 {!Get_Contact.Name} + ' '+ TEXT({!$Record.Reason__c}) + '  Year '+ TEXT({!Get_Contact.Stipend_Year__c})
)