+ Start a Discussion
Praneetha MurakondaPraneetha Murakonda 

How to update campaign member status ?

Hi All,

I'm doing a trigger on campaign, I have three record types and if the record type matches with custom setting I need to  create Campaign Member Status Records. As SortOrder field is required in Campaign Member Status I'm assigning the value to it from custom setting which has values [1,2,3,4]. While inserting as there will be two picklist values by default, it will get duplicate error. So I'm trying to either delete the picklist values or update the values. But, i'm not able to do it.

Please suggest

Thanks

Nagendra Prasad ChNagendra Prasad Ch
Add this code in Campaign After trigger. You no need to set any custom setting values, this code will take care of everything. No problem if you start the sortorder from 10 also, sortorder number should be greater than existing statuses sortorder value. No problem if u skip few numbers in between. For example, if existing values have sort order 1,2. no problem if you give sort order for ur values 5,6,7.
Try this code it will work.

 
List<CampaignMemberStatus> cms = new List<CampaignMemberStatus>();
        Set<Id> camps = new Set<Id>(); 
        List<CampaignMemberStatus> cms2Insert = new List<CampaignMemberStatus>();
        List<string> RemoveLabels = new List<string>();
        List<string> AddLabels = New List<string>();
        integer SortOrdr = 10;//Order must start from above 9, as cloned records already have 9 vaues in picklist
        
        Id RcType1Id = Schema.SObjectType.Campaign.getRecordTypeInfosByName().get('Record type 1').getRecordTypeId();
        Id RcType2Id = Schema.SObjectType.Campaign.getRecordTypeInfosByName().get('Record type 2').getRecordTypeId();
       
        
        for(Campaign cm : Trigger.new) {  
            if(cm.RecordTypeId == RcType1Id){             
                AddLabels.add('Attended');
                AddLabels.add('Registered');
                
                // Add existing status values to RemoveLabels, to delete at the end
				RemoveLabels.add('Sent');
                RemoveLabels.add('Responded');
                camps.add(cm.Id);
                
                for(String Lb : AddLabels){                    
                    CampaignMemberStatus CmpSt = new CampaignMemberStatus(CampaignId = cm.Id, HasResponded=true,Label = Lb, SortOrder = SortOrdr);
                    cms2Insert.add(CmpSt);
                    
                    SortOrdr++;   
                }
            }           
            
           
            else if(cm.RecordTypeId == RcType2Id ){
                
                AddLabels.add('Invited');
                AddLabels.add('Registered');
                AddLabels.add('Attended');
                AddLabels.add('Did Not Attend');
                AddLabels.add('Cancelled Registration');
                
                RemoveLabels.add('Sent');
                RemoveLabels.add('Responded');
                camps.add(cm.Id);
                
                for(String Lb : AddLabels){                    
                    CampaignMemberStatus CmpSt = new CampaignMemberStatus(CampaignId = cm.Id, HasResponded=true,Label = Lb, SortOrder = SortOrdr);
                    cms2Insert.add(CmpSt);
                    
                    SortOrdr++;   
                }
            }
            
            else{ // For remaining recordtypes
                
                AddLabels.add('Attended');
                AddLabels.add('Cancelled Registration');
                AddLabels.add('Did Not Attend');
                AddLabels.add('Invited');
                AddLabels.add('Registered');               
                
                RemoveLabels.add('Sent');
                RemoveLabels.add('Responded');
                //camps.add(cm.Id);
                
                for(String Lb : AddLabels){                    
                    CampaignMemberStatus CmpSt = new CampaignMemberStatus(CampaignId = cm.Id, HasResponded=true,Label = Lb, SortOrder = SortOrdr);
                    cms2Insert.add(CmpSt);
                    
                    SortOrdr++;   
                }
            }
        }
        system.debug('CampaignMemberStatus List : '+cms2Insert);
        insert cms2Insert;	// Insert new status values
        
        cms = [select Id, Label from CampaignMemberStatus where CampaignId IN :camps and Label in :RemoveLabels];
        for(CampaignMemberStatus status : cms)
        {
			// Before deleting existing statu, we need to make IsDefault = false for that status. If it  is default we cannot delete it
            status.IsDefault = false;
        }
        
        update cms;
        delete cms;// Delete unwanted Status

 
Nagendra Prasad ChNagendra Prasad Ch
Hi Praneetha,
Did above code work for you?. Is it what ur requirement?
Praneetha MurakondaPraneetha Murakonda

Hi Nagendra Prasad,

Thanks for your response. Actually my requirement is like if record type in campaign matches with the record type in my customsetting, then I need to create CampaignMemberStatus records with field mappings from custom setting. In Customsetting I have some Order__c field which has values like [1,2,3,4]. As there will be two default values in picklist, while inserting I need to delete the existing values from CampaignMemberStatus and I need to insert CampaignMemberStatus records with new status from customsetting and its Order..

If I insert the records with sortorder [1,2] will get the issue because by default the picklist will contain two values.

 

Thanks!