1. Write a trigger on Account, for update related contacts if Contact's Company Name field is blank, populate it with Account's Company Name field. Limitations: Only 1 loop allowed. Solution: trigger on Account (After Update) { Map<Id, Account> accountMap = Trigger.newMap; List<Contact> contactList = [Select Id, Company_Name__c From Contact Where AccountId in:accountMap.keys()]; List<Contact> contactListForUpdate = List<Contact>(); for(Contact contact : contactList) { if(String.isBlank(contact.Company_Name__c)) { contact.Company_Name__c = accountMap.get(contact.AccountId).Company_Name__c; contactListForUpdate.add(contact); } } update contactListForUpdate; }
Static methods, variables, and initialization code have these characteristics They’re associated with a class. They’re allowed only in outer classes. They’re initialized only when a class is loaded. They aren’t transmitted as part of the view state for a Visualforce page. Using Static Methods and Variables public class Parent { public static boolean isRun = true; } A trigger that uses this class could then selectively fail the first run of the trigger trigger T1 on Account (before delete, after delete, after undelete) { if(Trigger.isBefore){ if(Trigger.isDelete){ if( Parent .isRun){ Trigger.old[0].addError('Before Account Delete Error'); Parent .isRun =false; } ...