+ Start a Discussion
Abhinav AdminAbhinav Admin 

I Need a Trigger To Update Campaign Field (Status) Based On Lead Field (Rating == 'Hot'). Can any one please help to do this

I Need a Trigger To Update Campaign Field (Status) Based On Lead Field (Rating == 'Hot'). Can any one please help to do this...........



Thanks and Regards
Abhinav
Tejpal KumawatTejpal Kumawat
Hello Abhinav,

Use this code :
 
trigger LeadTrigger on Lead (after update) {
    Set<ID> leadIDs= new Set<ID>();
    for(Lead lead : Trigger.New){
        if(lead.Rating == 'Hot' && trigger.newMap.get(lead.Id).Rating != trigger.oldMap.get(lead.Id).Rating){
            leadIDs.add(lead.id);
        }
    }
    
    Set<ID> campaignIDs = new Set<ID>();
    for(CampaignMember cm : [SELECT CampaignID, LeadID, Status FROM CampaignMember WHERE LeadID IN : leadIDs]){
        campaignIDs.add(cm.CampaignID);
    }


    list<Campaign> listCampaign = new list<Campaign>();
    for(Campaign c : [select Id, Status from Campaign where Id IN : campaignIDs ]){
        c.Status = 'In Progress';
        listCampaign.add(c);
    }
    
    if(listCampaign.size() > 0)
        update listCampaign;
}

If this answers your question then hit Like and mark it as solution!
Abhinav AdminAbhinav Admin
Thank You Tejpal Kumawat..............


Regards,
Abhinav
Tejpal KumawatTejpal Kumawat
Good Abhinav,

If this answers your question mark Best Answer it as solution and then hit Like!