Skip to content

Instantly share code, notes, and snippets.

@msimonis
Created July 14, 2014 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msimonis/c283c4f8c5724bbd9ede to your computer and use it in GitHub Desktop.
Save msimonis/c283c4f8c5724bbd9ede to your computer and use it in GitHub Desktop.
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