0

I have created record trigger flow on account object. my requirement is when specific filed is update on the account record then all related custom object record status goes change. Now, on custom object records have some validation rules, triggers, flows as well. so i want error handling in record trigger flow. let say 10 records in record update and 7 sucess and 3 fail then 7 sucessful record update and for 3 error mail goes to admins. how do i achive this requirement

i'm expecting try catch functionality in record trigger flow

2 Answers 2

0

For example : if we are implementing this with an Apex Trigger :

trigger AccountTrigger on Account (after update) { List<Custom_Object__c> customObjectsToUpdate = new List<Custom_Object__c>();

for (Account acc : Trigger.new) {
    if (acc.Custom_Field__c != Trigger.oldMap.get(acc.Id).Custom_Field__c) {
        List<Custom_Object__c> relatedCustomObjects = [SELECT Id, Status__c FROM Custom_Object__c WHERE Account__c = :acc.Id];
        
        for (Custom_Object__c obj : relatedCustomObjects) {
            obj.Status__c = 'Updated'; // Example update logic
            customObjectsToUpdate.add(obj);
        }
    }
}

try {
    update customObjectsToUpdate;
} catch (Exception e) {
    // Handle exceptions, notify admins via email
    String errorMessage = 'Error updating Custom Objects: ' + e.getMessage();
    System.debug(errorMessage);
    // Send email notification to admins using Apex Email Services or custom logic Error Handling: Notify admins via email when there are errors, such as validation rule failures on the Custom Object records.
}

}

Sign up to request clarification or add additional context in comments.

1 Comment

And how exactly this will work in "save what you can" mode? You caught the exception cool but no change went through
0

I'm not aware of option that would let you do this for flows. On error it'll rollback the whole operation.

You will need to write a piece of Apex code that can be called from flow (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm)

and that code would use List<Database.SaveResult> results = Database.update(records, false);

(the "Database" version of update means it doesn't throw errors anymore. And the second parameter works as "save what you can"). You'd then loop through the results, check if not success - add them to some string variable and eventually build your email's body. https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_database_saveresult.htm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.