Created
July 6, 2018 08:12
-
-
Save sfcure/23d3f924043229c3f9c4edbcbca00521 to your computer and use it in GitHub Desktop.
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
public class ContactTriggerHandler extends TriggerHandler { | |
public override void beforeInsert() { | |
// Call the static method to populate the address | |
ContactTriggerHandler.populateMailingAddress( (List<Contact>) Trigger.new ); | |
} | |
public override void beforeUpdate() { | |
} | |
// method which will copy the mailing address from the account's billing address | |
public static void populateMailingAddress( List<Contact> lstContacts ) { | |
// Get the account Ids | |
Set<Id> lstAccountIds = new Set<Id>(); | |
for( Contact con : lstContacts ) { | |
if( con.AccountId != null ) { | |
lstAccountIds.add( con.AccountId ); | |
} | |
} | |
// Fetch all the account records and create a map of the records with their respective ids | |
Map<Id, Account> mapAccounts = new Map<Id, Account> ( [ | |
SELECT | |
Id, BillingStreet, BillingCity, BillingState, BillingCountry, BillingPostalCode | |
FROM | |
Account | |
WHERE | |
Id IN :lstAccountIds | |
] ); | |
// Populate mailing address of the contact records | |
for( Contact con : lstContacts ) { | |
if( con.AccountId != null ) { | |
Account acc = mapAccounts.get( con.AccountId ); | |
con.MailingStreet = acc.BillingStreet; | |
con.MailingCity = acc.BillingCity; | |
con.MailingState = acc.BillingState; | |
con.MailingCountry = acc.BillingCountry; | |
con.MailingPostalCode = acc.BillingPostalCode; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment