Skip to content

Instantly share code, notes, and snippets.

@brianmfear
Created October 28, 2020 16:31
Show Gist options
  • Save brianmfear/4a773e2d516aee16d9c1728c4a3cb800 to your computer and use it in GitHub Desktop.
Save brianmfear/4a773e2d516aee16d9c1728c4a3cb800 to your computer and use it in GitHub Desktop.
trigger UpdateRelatedContact on Account (after update) {
Map<Id, Contact> contacts = new Map<Id, Contact>();
for(Integer i = 0, s = Trigger.new.size(); i < s; i++) {
Account oldRecord = Trigger.old[i], newRecord = Trigger.new[i];
if(
oldRecord.Field1 != newRecord.Field1 ||
oldRecord.Field2 != newRecord.Field2 ||
oldRecord.Field3 != newRecord.Field3
) {
contacts.put(newRecord.Id, new Contact(
Field1 = newRecord.Field1,
Field2 = newRecord.Field2,
Field3 = newRecord.Field3
));
}
}
for(Contact record:[
SELECT AccountId
FROM Contact
WHERE AccountId = :contacts.keySet()
]) {
contacts.get(record.AccountId).Id = record.Id;
}
upsert contacts.values(), Contact.Id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment