Skip to main content
I am looking for the best way to accomplish updating Campaign Members Custom Field when an Event is added to the Campaign.

I don;t think its possible with process flow.  Any Trigger examples would be greatly appreciated.
7 个回答
  1. 2016年3月4日 17:54

    Hi Ireed,

    Yup the code will work for bulk data, there is a little bit modification with the recordType is needed, See the updated code below

     

    trigger UpdateCampaignMember on Event (before insert) {

    Set<Id> campaignId = new Set<Id>();

    String Recordtype;

    //create a map between record type name and id for easy retrieval

    Map<String, String> EventRecordTypes;

    List<RecordType> rtypes = [Select Name, Id From RecordType

    where sObjectType='Event' and isActive=true];

    EventRecordTypes = new Map<String,String>{};

    for(RecordType rt: rtypes){

    EventRecordTypes.put(rt.Name,rt.Id);

    }

    //////////////////////////////////////////////////

    for(Event e : trigger.new){

    String whatId = e.WhatId+'';

    Recordtype = e.RecordTypeId;

    if(whatId.startsWith('701') && Trigger.isInsert && Recordtype == EventRecordTypes.get('ID INS Event')){

    // COLLECTING ALL campaign IDs on which event is created.

    // These campaigns are filtered with RecordType

    campaignId.add(e.WhatId);

    }

    }

    //IN THE campaignId you have campaign Ids

    if(campaignId.size()>0){

    // FETCHING ALL Campaign members.

    List<campaignMember> cMemberToUpdate = [SELECT id FROM campaignMember WHERE campaignId IN :campaignId];

    for(campaignMember cMember : cMemberToUpdate){

    cMember.InsCheckIn__c = True;

    // UPDATE YOUR CUSTOM FIELD HERE.

    }

    update cMemberToUpdate;

    }

    }

    Please have a look on this , I hope this will help.