+ Start a Discussion
KevSnellKevSnell 

Lead score formula based on Campaign Member Status

Hi all,

 

I have created Campaign Member number field (Campaign_Active__c) which is either 1(active) or -1(inactive) depending on whether the campaign is active.  This updates when the Campaign is changed from active to inactive.

I now need to create a trigger which adds or subtracts a score from the lead scoring field (Campaign_Score__c) depending on the Campaign_Active__c.

 

I did a search and found some code and tried modifying it but currently am not being very succesfully so an help would be appreciated.  

 

trigger CampaignMemberAfter on CampaignMember(after insert, after update, after delete)
{
    Map<Id,CampaignMember> campaignLeads = new Map<Id,CampaignMember>{};
    List<CampaignMember> camMembers = trigger.IsInsert || trigger.IsUpdate ? trigger.new : trigger.old;
    
    for(CampaignMember cm : camMembers)
        campaignLeads.put(cm.LeadId, cm);
 
    List<Lead> consToUpdate = new List<Lead>{};
 
    for(Lead con : [Select Id, Campaign_Score__c from Lead where Id in :campaignLeads.keySet()])
    {
        if(trigger.isInsert || trigger.isUpdate) 
        con.Campaign_Score__c += campaignLeads.get(con.Id).Campaign_Active__c;
 
        else if (trigger.isDelete)
        con.Campaign_Score__c += campaignLeads.get(con.Id).Campaign_Active__c;
 
        consToUpdate.add(con);
    }
}

 

 

 

KevSnellKevSnell

Ok I think I found the first issue which is the trigger won't fire as it's looking at a forumla field which is always read only so I need to create a trigger on campaign to update all Members when Campaign is changed to Active or InActive.