+ Start a Discussion
KevSnellKevSnell 

Campaign Trigger help to Update Campaign Member

Hi all,

 

I have a problem with the below trigger and need some help.

 

This is what the trigger shoud do:

 

  1. If Campaign.IsActive = True then change CampaignMember.Campaign_Acitve__c = True
  2. If Campaign.IsActive = False and the CampaignMember.Campaign_Acitve__c = New then change CampaignMember.Campaign_Acitve__c = FalseUpdate
  3. If Campaign.IsActive = False and current CampaignMember.Campaign_Acitve__c != New then change CampaignMember.Campaign_Acitve__c = False

However I can't get 2 & 3 to work, here's my code:

 

trigger trgr_Campaign_ActiveCampaign_au  on Campaign (after update) {

    CampaignMember[] CampaignMembersRelatedToCampaignBatch = [ select CampaignID, Campaign_Active__c from CampaignMember where CampaignId in :Trigger.new ];

    for(CampaignMember currentCampaignMember : CampaignMembersRelatedToCampaignBatch ) {
        Campaign CampaignMemberParentCampaign = Trigger.newMap.get( currentCampaignMember.CampaignID ); 
        
        if(CampaignMemberParentCampaign.IsActive == True) {
            currentCampaignMember.Campaign_Active__c = 'True';
        }

        if(CampaignMemberParentCampaign.IsActive == False && currentCampaignMember.Campaign_Active__c == 'New') {
            currentCampaignMember.Campaign_Active__c = 'FalseUpdate';
        }
        
        if(CampaignMemberParentCampaign.IsActive == False && currentCampaignMember.Campaign_Active__c != 'New') {
            currentCampaignMember.Campaign_Active__c = 'False';
        } 
        
    }
      
    update CampaignMembersRelatedToCampaignBatch;

}

 

Any help would really be appreciated.

Thanks

Kev 

 

 

nick1505nick1505

Hi,

 

Try the below code:

 

trigger trgr_Campaign_ActiveCampaign_au on Campaign (after update)
{

Map<Id, List<CampaignMember>> mapCampaignMem = new Map<Id, List<CampaignMember>>();
List<CampaignMember> lstUpdateCampaignMember = new List<CampaignMember>();
for(CampaignMember objCM : [select CampaignID, Campaign_Active__c from CampaignMember where CampaignId in :Trigger.new])
{
if(!mapCampaignMem.containsKey(objCM.CampaignID))
mapCampaignMem.put(objCM.CampaignID , new List<CampaignMember> ({objCM}) );
else
mapCampaignMem.get(objCM.CampaignID).add(objCM);
}

for(Campaign objCampaign : Trigger.new)
{
if(mapCampaignMem != null && mapCampaignMem.containsKey(objCampaign) && mapCampaignMem.get(objCampaign) != null)
{
List<CampaignMember> lstCampainMember = mapCampaignMem.get(objCampaign);
for(CampaignMember objCM : lstCampainMember)
{
if(objCampaign.IsActive)
objCM.Campaign_Active__c = 'True';

if(!objCampaign.IsActive && objCM.Campaign_Active__c == 'New')
objCM.Campaign_Active__c = 'FalseUpdate';
if(!objCampaign.IsActive && objCM.Campaign_Active__c != 'New')
objCM.Campaign_Active__c = 'False';

lstUpdateCampaignMember.add(objCM);
}
}

}
if(lstUpdateCampaignMember != null && lstUpdateCampaignMember.size() >0)
update lstUpdateCampaignMember;

}

 

Let me know if incase of any issues!

 

 

SRKSRK

Campaign_Active__c is a text field ??

& also put some system.debug & check the value of "CampaignMemberParentCampaign.IsActive"