Created
May 18, 2020 21:14
-
-
Save AmbreJuryeaAmole/277558fff072626412f1418b829919b2 to your computer and use it in GitHub Desktop.
Trigger Good example with try/catch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger CaseTrigger on Case (after insert) { | |
system.debug('***CaseTrigger begin'); | |
// In this example trigger we are simply wanting to flag at the Account level | |
// that the Account has a Case linked to it, which is a lookup field on Case | |
// so we wouldn't be able to do a rollup count. | |
// In reality we would also want to track deletes of Cases but that is skipped | |
// for demonstration purposes. | |
// bulk map of accounts that we need to update | |
map<Id, Account> accountsForUpdateMap = new map<Id, Account>(); | |
// find accounts to update | |
for(Case c : trigger.new){ | |
// if this case is linked to an account and we've not already tagged this account | |
if(c.AccountId != null && !accountsForUpdate.containsKey(c.AccountId)) | |
// add this account to the map | |
accountsForUpdate.put(c.AccountId, new Account(Id = c.AccountId, HasACase__c = true)); | |
} | |
// check to see if we need to perform DML | |
if(!accountsForUpdate.isEmpty()){ | |
try{ | |
// let's do the DML | |
update accountsForUpdate.values(); | |
} catch(dmlexception e){ | |
// ************ Assume the ACCOUNT update FAILED *********** | |
// If DML failed, ends up here | |
system.debug('Error updating account: ' + e); | |
// Let's force the database to roll-back by adding an error to these records just | |
// like salesforce would normally do. Our error can be more descriptive though. | |
// Build a map of Case trigger rows keyed on the Account Id | |
map<id, list<case>> accountIdCaseMap = new map<id, list<case>>(); | |
for(Case c : trigger.new){ | |
// if this case is linked to an account that failed | |
if(c.AccountId != null) | |
// if we already found this account and this is another case, add this | |
// case to that list | |
if(accountIdCaseMap.containsKey(c.AccountId)) | |
accountIdCaseMap.get(c.AccountId).add(c); | |
// Add this account id with a new list of Case | |
else | |
accountIdCaseMap.put(c.AccountId, new list<case>(c)); | |
} | |
// Loop through the errors and tag the proper cases | |
for ( Integer i = 0; i < e.getNumDml(); i++ ){ | |
// for this dml error, get the id of the failed account, tag the matching cases. | |
for(Case c : accountIdCaseMap.get(e.getDmlId( i ))) | |
// add an error to this row, which will prevent this row from being updated | |
c.addError('Unable to update linked Account record due to an error: ' + | |
e.getDmlMessage( i )); | |
} | |
} | |
} | |
// If we get this far in code, something may have failed and been caught above, or everything worked. | |
system.debug('Potential Success'); | |
// If anything were to go wrong in the above DML, since we do have a try/catch the server | |
// will NOT automatically rollback the changes. But since we have used the .addError method on the | |
// correct trigger rows we have effectively prevented the case inserts. | |
// Our error would show to the user on the standard page or to the calling code that fired the trigger. | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment