7

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?

1
  • CampaignMember is a simple relationship between a Contact or Lead with a Campaign object. What information about the CampaignMember are you trying to detect a change in? Or are you referring to a change to the Contact or Lead record??? Commented Jul 31, 2012 at 20:30

2 Answers 2

10

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;
}
0
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .