top of page

UseCase: Deletion of associated contact for a account except the primary contacts



//UseCase for Deletion of associated contact for a account except the primary contacts and Clarity on Trigger.Old and Trigger.New Utilisation in Code trigger accountTrigger on Account ( before delete )

{ Set<Id> accountId = new Set<Id>() ; // Used to Store the Account id's from Trigger.OLD which will be used in SOQL for fetching contact records System.debug ( Trigger.Old ) ; // Trigger.Old used in Delete because in salesforce Trigger.New Would not have any value for deletion as no data value is modified when a delete happens in Object hence Trigger.Old which captures the unmodified values slated for deletion for ( Account acct : Trigger.Old )

{ accountId.add ( acct.Id ); }

System.debug ( accountId ) ; // Validates the accountId set , System.Debug shouldn't be included in code to be deployed to production List<contact> contactDel = new List<contact>(); if ( accountId.size()>0 ) { contactDel = [ Select Id

from Contact

where Account.Id IN: accountId AND isPrimary__c = True ]; } delete contactDel; }



51 views0 comments

Recent Posts

See All
bottom of page