Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
Hi there,

I have a object Campaign_Follow_Up_Stage__c ,on this object i have a Campaign Lookup.

* On Campaign_Follow_Up_Stage__c object, i have a status field, when i insert or update record,if status = "Planned" , It changed the Campaign Field status into "In-Progress",If if status = "Draft" , It changed the Campaign Field status into "Planned" and if status = "Compelete" , It changed the Campaign Field status into "Compelete".

I wrote this:-

trigger updateCampaignStatus on Campaign_Follow_Up_Stage__c (after Update,after insert) {

    set<Id> campIds = new set<Id>(); 

        for(Campaign_Follow_Up_Stage__c c : trigger.new){ 

         if(c.Status__c == 'Planned'){

            campIds.add(c.id);

        }     

    }

    List<Campaign> cmList = [SELECT id, status FROM Campaign WHERE id In :campIds];

     for(Campaign cm : cmList){

         cm.status = 'In Progress';

     }

     update cmList;

}

Any help appreciated!

Thanks

L.N

 
2 answers
  1. Jun 26, 2020, 3:45 PM

    hi, try this:

    trigger updateCampaignStatus on Campaign_Follow_Up_Stage__c (after Update,after insert) {

    set<Id> campIds = new set<Id>();

    Map<Id,String> statusByIdMap = new Map<Id,String>();

    for(Campaign_Follow_Up_Stage__c c : trigger.new){

    if(c.Status__c == 'Planned' || c.Status__c == 'Draft' || c.Status__c == 'Compelete'){

    campIds.add(c.id);

    statusByIdMap.put(c.id,c.Status__c );

    }

    }

    List<Campaign> cmList = [SELECT id, status FROM Campaign WHERE id In :campIds];

    for(Campaign cm : cmList){

    if(statusByIdMap.get(cm.id) == 'Planned'){

    cm.status = 'In Progress';

    }

    if(statusByIdMap.get(cm.id) == 'Draft'){

    cm.status = 'Planned';

    }

    if(statusByIdMap.get(cm.id) == 'Compelete'){

    cm.status = 'Compelete';

    }

    }

    update cmList;

    }

    If it solves your issue please mark as correct so it can help others asking the same.

    Good luck!
Loading
0/9000