You need to sign in to do that
Don't have an account?

Update Account and Contact Checkbox from custom object trigger
So, I have an object called Interaction. When the picklist value "Speaker" is stored in the Interaction object's Selected_Sub_type__c field, I want it to update Contact and Account object checkboxes. It works correctly for the Contact object. I want to enhance the following code so that it will also update the Account object record Guest_Speaker_s__c checkbox to TRUE.
The Interaction object is related to the Account and Contact object by these fields:
Field Label API Name Data Type
Account Account__c Master-Detail(Account)
Contact Contact__c Lookup(Contact)
This code works to update the Contact object checkbox:
======================================================================================
// Update Contact field: "Guest_Speaker__c" to TRUE when a new Interaction__c record is inserted or updated.
// This code will not set the checkbox to false if the Sub_type value is not 'Speaker'
trigger UpdateGuestSpeakerCheckbox on Interaction__c (after insert, after update) {
// Will store Contact record ID
map< id, contact > contacts = new map< id, contact >();
// Create trigger for new or selected Interaction__c record
for(Interaction__c record:trigger.new)
if(record.Selected_Sub_type__c == 'Speaker')
// Update checkbox field on the Contact record to TRUE
contacts.put(record.contact__c, new contact(id=record.contact__c, Guest_Speaker__C = TRUE));
update contacts.values();
}
======================================================================================
This was my attempt to make the checkbox on the Account record update with the same trigger:
The Contact record updated fine. The Account record was not updated.
Can you help me dial this in?
Thanks!
Kevin
My attempt, it compiles but it does not update Account object.
=======================================================================================
// Update Contact field: "Guest_Speaker__c" to TRUE when a new Interaction__c record is inserted or updated.
// This code will not set the checkbox to false if the Sub_type value is not 'Speaker'
trigger UpdateGuestSpeakerCheckbox on Interaction__c (after insert, after update) {
// Will store Contact record ID
map< id, contact > contacts = new map< id, contact >();
// Create trigger for new or selected Interaction__c record
for(Interaction__c record:trigger.new)
if(record.Selected_Sub_type__c == 'Speaker')
// Update checkbox field on the Contact record to TRUE
contacts.put(record.contact__c, new contact(id=record.contact__c, Guest_Speaker__C = TRUE));
update contacts.values();
// The new section added to update the Account object, just doesn't work!
// Store Account record ID
map< id, account > account = new map< id, account >();
for(Interaction__c record:trigger.new)
if(record.Selected_Sub_type__c == 'Speaker')
// Update checkbox field on the Account record to TRUE
Account.put(record.account__c, new account(id=record.account__c, Guest_Speaker_s__c = TRUE));
}
=======================================================================================
Since the Interaction record is related to both the Contact and Account then you can simplify your trigger logic like so:
All Answers
I don't see the update statement for account. Also you should change the account Map variable name to 'accounts'.
// Store Account record ID
map< id, account > accounts = new map< id, account >();
for(Interaction__c record:trigger.new)
if(record.Selected_Sub_type__c == 'Speaker')
// Update checkbox field on the Account record to TRUE
accounts.put(record.account__c, new account(id=record.account__c, Guest_Speaker_s__c = TRUE));
}
update accounts.values();
I have noticed that your update statement for contact is within your for loop which is not good. Also you don't need to have two for loops for Contacts and Accounts separately.
Let me know if it helps.
Since the Interaction record is related to both the Contact and Account then you can simplify your trigger logic like so: