-
-
Save msimonis/c283c4f8c5724bbd9ede 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
trigger PersonContact on Account (after insert, after update) { | |
List<Account> personAccountsToUpdate = new List<Account>(); | |
for (Account newAccount : newAccounts) { | |
// Person Account fields can't be | |
// referenced directly in packages. | |
Id pcId = (Id) newAccount.get('PersonContactId'); | |
if (pcId != null && newAccount.Person_Contact__c == null) { | |
// Update only what needs to be updated. | |
// Including the entire new account record | |
// may have unintended consequences. Also, | |
// a select isn't needed and wastes a query. | |
Account paToUpdate = new Account( | |
Id = newAccount.Id, | |
Person_Contact__c = pcId | |
); | |
personAccountsToUpdate.add(paToUpdate); | |
} | |
} | |
if (personAccountsToUpdate.size() > 0) { | |
update personAccountsToUpdate; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment