+ Start a Discussion
Sindhu NagabhushanSindhu Nagabhushan 

Update Campaign Member Status

Hi,
I have written the below trigger to update the campaign member status. But its not working and i am not able to figure out why
Thanks for help in advance.

trigger UpdateCampaignStatus on FeedBack__c (after insert,after update) {
    try{
    set<id> fid=new set<id>();
    for(feedback__c fb:trigger.new)
    {
        if(fb.recordtypeid=='01250000000UNZH')
        {
            list <CampaignMember> cm=[select id,status from CampaignMember where contactid=:fb.contact__c and campaignid=:fb.Campaign__c];
            cm[0].status='Completed';
            Update cm[0];
        }     
    }
    }catch(exception e)
    {
        system.debug(e.getmessage());
    }
        }
SFDCboxSFDCbox
Could you please post the error message? Please note your tyrigger is not bulkified either nad you are hard coding the recordtype id.
Vishal Negandhi 16Vishal Negandhi 16
Yes, this trigger isn't following any of the good practices. 

It has a hard coded record type id - will fail when you deploy this. 
It has a SOQL inside a for - will fail for bulk operations
It has a DML inside a for - will fail for bulk operations
It will only process the FIRST record when bulk operations take place. 

Now, coming to the error, are you sure you getting any record returned from this query
list <CampaignMember> cm=[select id,status from CampaignMember where contactid=:fb.contact__c and campaignid=:fb.Campaign__c];

I believe there is no data coming here and hence no updation.
Sindhu NagabhushanSindhu Nagabhushan
Hi,

I am not getting any error. When i use system.debug before Update statement, I get status=completed.

list <CampaignMember> cm=[select id,status from CampaignMember where contactid=:fb.contact__c and campaignid=:fb.Campaign__c];
            cm[0].status='Completed';
system.debug(cm[0]);
            Update cm[0];

But the status is not reflecting in my UI
Sindhu NagabhushanSindhu Nagabhushan
I have written the below code and it is working. I am writing trigger 1 st time.. Some1 please help me to bulkify this trigger. Thank you very much for your help in advance.

trigger UpdateCampaignStatus on FeedBack__c (after insert,after update) {
    try
    {
    
        Id Rid = Schema.SObjectType.Feedback__c.getRecordTypeInfosByName().get('Relationship').getRecordTypeId();
        
        map<string,id> memberstatus=new map<string,id>();
    
        for(feedback__C fb:trigger.new)
        {
            if(fb.recordtypeid==rid && fb.Contact__c<>null && fb.Campaign__c<>null)
            {
                   for(campaignmemberstatus cms:[select label,id from campaignmemberstatus where campaignid=:fb.Campaign__c])
                   memberstatus.put(cms.label, cms.id);
            
                integer i=memberstatus.size();
            
                if(!(memberstatus.containsKey('Entered')))
                {
                    CampaignMemberStatus cms1 = new CampaignMemberStatus(CampaignId=fb.Campaign__c, HasResponded=true, Label='Entered',SortOrder=i+1);
                     insert cms1;
                }
                   CampaignMember cm=[select id,status from CampaignMember where contactid=:fb.contact__c and campaignid=:fb.Campaign__c];
                cm.status='Entered';
                Update cm; 
            }
    }
    }catch(exception e)
    {
    system.debug(e.getMessage());
    system.debug(e.getLineNumber());
    }
}
SFDCboxSFDCbox
Something like this :

    trigger UpdateCampaignStatus on FeedBack__c (after insert,after update) {
   
    
        Id Rid = Schema.SObjectType.Feedback__c.getRecordTypeInfosByName().get('Relationship').getRecordTypeId();
        
        map<string,id> memberstatus=new map<string,id>();
        Map<Id,feedback__c> fbRecordsMap = new Map<Id,feedback__C>();
        Set<Id> campaignIds = new Set<Id>();
        Set<Id> contactIds = new Set<Id>();
        List<campaignmemberstatus> campaignMemeberStatusToBeInsertedList = new List<campaignmemberstatus>();
        List<campaignMemeber> campaignMemberToUpdateStatusList= new List<campaignMemeber>();
        for(feedback__C fb:trigger.new)
        {
            if(fb.recordtypeid==rid && fb.Contact__c<>null && fb.Campaign__c<>null){ 
                campaignIds.add(fb.Campaign__c);
                contactIds.add(fb.Contact__c);                
            }
        }        
                   
        for(campaignmemberstatus cms:[select label,id from campaignmemberstatus where campaignid IN:campaignIds AND label!= 'Entered']){
                   memberstatus.put(cms.label, cms.id);
                   integer i=memberstatus.size();
                    CampaignMemberStatus campaignMemeberStatusToBeInserted = new CampaignMemberStatus(CampaignId=fb.Campaign__c, HasResponded=true, Label='Entered',SortOrder=i+1);
                    campaignMemeberStatusToBeInsertedList.add(campaignMemeberStatusToBeInserted);
        }
                   
        for (CampaignMember campaignMemeberToUpdateStatus :[select id,status from CampaignMember where contactid IN :contactIds and campaignid IN :campaignIds]){}
                campaignMemeberToUpdateStatus.status='Entered';
                campaignMemberToUpdateStatusList.add(campaignMemeberToUpdateStatus); 
        }
        try{
            if(campaignMemeberStatusToBeInsertedList != null && campaignMemeberStatusToBeInsertedList.size() >0){insert campaignMemeberStatusToBeInsertedList }
            if(campaignMemberToUpdateStatusList != null && campaignMemberToUpdateStatusList.size() > 0){update campaignMemberToUpdateStatusList} 
            }catch(exception e){
                system.debug(e.getMessage());
                system.debug(e.getLineNumber());
            }
    }