function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
laxmi narayan 11laxmi narayan 11 

Update Campaign Status from another object Status using trigger

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



 
Best Answer chosen by laxmi narayan 11
Agustin BAgustin B
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!

All Answers

Agustin BAgustin B
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!
This was selected as the best answer
laxmi narayan 11laxmi narayan 11
Hi Agustin B,

Thank you soo much.

Thanks 
L.N