+ Start a Discussion
BritishBoyinDCBritishBoyinDC 

Campaign Member Status Trigger

So I am seeing some odd behavior with a Campaign/CampaignMemberStatus trigger

 

I remove the Standard Sent and Responded, and replace them with some new ones. But to avoid problems with cloning, I check to see if the Campaign already has my new custom values, and don't update cloned records...

 

So if the default value I use  is 'Added to Campaign', it fails...but if I change the new default to 'Added to the Campaign' (or as far as I can tell, any other phrase) it works just fine...

 

Is 'Added to Campaign' some special phrase I should be avoiding?

Shilpa_SFShilpa_SF

Hi,

 

   Could you please provide the Code for the Trigger, you wrote on the Campaign/Campaign Member, I replaced one of the  picklist value in the Campaigm Member Status with "Added to Campaign" its working fine. This information is required to understand your issue in detail, so that i  could help you in providing the solution.

BritishBoyinDCBritishBoyinDC

Thanks for checking. I tried the code in a different instance, and you're correct - it works fine, but I can't see any reasons (e.g. workflow, other triggers etc) why this would be having difficulties in this particular instance...?

 

 

trigger UpdateCampaignMemberStatus1 on Campaign (after insert) {
    List<CampaignMemberStatus> newCampaignMemberStatus = new List<CampaignMemberStatus>();
    List<CampaignMemberStatus> oldCampaignMemberStatus = new List<CampaignMemberStatus>();
    Set<String> checkdefaultstatus = new Set<String>();
    Set<Id> removecids = new Set<Id> ();
    
    For (CampaignMemberStatus excms: [Select Id, CampaignId, Label from CampaignMemberStatus where CampaignId IN :trigger.new]){
    checkdefaultstatus.add(excms.CampaignId + excms.Label); 
    }
 
    for (Campaign c : trigger.new) {
        // Add campaign ids to list for removal of campaign member status values
        if (checkdefaultstatus.contains(c.Id + 'Sent')){
        removecids.add(c.id);      
        }
           if (!checkdefaultstatus.contains(c.Id + 'Added To Campaign')) {// Add campaign member status for "Added To Campaign"
            newCampaignMemberStatus.add (new CampaignMemberStatus(
            CampaignId=c.id,
            HasResponded=FALSE,
            IsDefault=TRUE,
            Label='Added To Campaign',
            SortOrder=3));
            }
          
          if (!checkdefaultstatus.contains(c.Id + 'Communication Sent')) {// Add campaign member status for "Communication Sent"
        newCampaignMemberStatus.add (new CampaignMemberStatus(
        CampaignId=c.id,
        HasResponded=FALSE,
        IsDefault=FALSE,
        Label='Communication Sent',
        SortOrder=4)); 
        }
                 
}//end loop
                
    // SOQL query to retrieve the default campaign member status values which will be removed after the new values are inserted.
    oldCampaignMemberStatus = [select cms.id from CampaignMemberStatus cms where cms.CampaignId in :removecids];
    insert newCampaignMemberStatus;
    delete oldCampaignMemberStatus;
} //end trigger

 

 

jkucerajkucera

Looks about right by me.