Reputation: 3
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
Upvotes: 0
Views: 147
Reputation: 19637
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
Upvotes: 0
Reputation: 1
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.
}
}
Upvotes: 0