Is there a way to trigger a field update such that when the information about a campaign member is modified in the campaign object, it updates a field of the same member's contact record?
Attribution to: rahi
Possible Suggestion/Solution #1
jordan's trigger is good in concept, but would probably violate Governor Limits if it were executed in bulk. A good design principle is to avoid having database operations inside of for
loops, to avoid running lots of queries.
Here's a modified form which allows bulk operations:
trigger campaignmember on CampaignMember (after update) {
// Query for all the contact information
List<Id> contactsIds = new List<Contact>{};
for(CampaignMember member : trigger.new) {
contactIds.add(member.contactId);
}
List<Contact> contactsOld = [SELECT Id FROM Contact WHERE Id in :contactIdsToUpdate];
// Change all the contact records
List<Contact> contactsNew = new List<Contact>{};
for (Contact c : contactsOld) {
// perform some update to contact record
contactsNew.add(c);
}
// Update all the queries at once
update contactsNew;
}
Attribution to: Benj
Possible Suggestion/Solution #2
A fairly simple trigger (depending on what you're trying to do) can help with that. Here's an example:
trigger campaignContactUpdate on campaignMember (after update) {
list<contact> contactsToUpdate = new list<contact>();
for(campaignmember cm : trigger.new) {
if(cm.contactid != null && cm.some_custom_field__c == 'somevalue') cm.contact.some_other_field__c = 'newvalue';
contactsToUpdate.add(cm.contact);
}
try { update contactstoUpdate; }
catch(dmlexception e) { system.debug('Error with trigger! ' + e); }
}
Needless to say you'd replace the placeholders (some_custom_field__c, somevalue, etc) with whatever you're actually interested in.
If you provide some more info I could try to make it more relevant for your situation.
Attribution to: adjabry
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3