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 

Campaign Status

Hi there, 
I have an object Campaign_Follow_Up_Stage__c, On this object, I have a Campaign Lookup.
On Campaign_Follow_Up_Stage__c object, there are some record which status is "completed", If the status of any of these records 
changes, supposed I changed the status of one record  "Completed" to "Planned" then Campaign status field changed 
the value "In-progress".
* If all records status is completed then-campaign status field value is Completed.
* If all records status is Planned then-campaign status field value is In-progress.
* If all records status is Draft then-campaign status field value is Planned.
* If one of it's record status is planned then-campaign status field value is In-progress.
* If one of it's record status is Draft then-campaign status field value is planned.


trigger updateCampaignStatus on Campaign_Follow_Up_Stage__c (after Update,after insert) {
//list<Campaign_Follow_Up_Stage__c> cList = new list<Campaign_Follow_Up_Stage__c>(); 
    set<Id> campIds = new set<Id>();   
        for(Campaign_Follow_Up_Stage__c c : trigger.new){  
          campIds.add(c.Campaign__c);  
        }     
   List<Campaign> cmList = new List<Campaign>();
    for(Campaign cam:[SELECT Id,(SELECT Id,Name,status__c ,Campaign__c FROM Campaign_Follow_Up_Stages__r order by Name) FROM Campaign WHERE Id IN :campIds]){
        for(Campaign_Follow_Up_Stage__c c:cam.Campaign_Follow_Up_Stages__r){
             if(c.status__c == 'Completed'){
                 cam.status ='Completed';
             }
             if(c.status__c =='Planned'){
                 cam.status ='In Progress';
             }
             if(c.status__c =='Draft'){
                 cam.status ='Planned';
             }
             cmList.add(cam);
        }
    }
     update cmList;
}


Any help appreciated

Thanks 
L.N
Jitendra Singh ShahiJitendra Singh Shahi

Hi,
Please try below code, As I am unable to understand which case has a priority higher :
* If one of it's record status is planned then-campaign status field value is In-progress.
* If one of it's record status is Draft then-campaign status field value is planned.
so I assumed if any of the record's status is draft than campaign should be planned and if no record in the draft but any one record is in planned then-campaign should be in-progress.
 

trigger updateCampaignStatus on Campaign_Follow_Up_Stage__c (after Update,after insert) {
    list<Campaign_Follow_Up_Stage__c>(); 
    set<Id> campIds = new set<Id>();   
    for(Campaign_Follow_Up_Stage__c c : trigger.new){  
      campIds.add(c.Campaign__c); 
    }     
   List<Campaign> cmList = new List<Campaign>();
    for(Campaign cam:[SELECT Id,(SELECT Id,Name,status__c ,Campaign__c FROM Campaign_Follow_Up_Stages__r order by Name) FROM Campaign WHERE Id IN :campIds]){
         Boolean isCompleted = false;
         Boolean isPlanned = false;
         Boolean isDraft = false;
        for(Campaign_Follow_Up_Stage__c c:cam.Campaign_Follow_Up_Stages__r){
             if(c.status__c == 'Completed'){
                 isCompleted = true;
             }
             if(c.status__c =='Planned'){
                 isPlanned = true;
             }
             if(c.status__c =='Draft'){
                 isDraft = true;
             }
        }
        if(isDraft){
            cam.status = 'Planned';
        }else if(isPlanned && !isDraft){
            cam.status = 'In Progress';
        }else if(isCompleted && !isPlanned && !isDraft){
            cam.status = 'Completed';
        }
        cmList.add(cam);
    }
     update cmList;
}

let me know if you have any issues. If it fixed your problem please mark it as the best answer, it will help others having the same issue.