I have a 'Highest Priority' field(declarative rollup summary field) on contact object. I have some campaigns namely Red, Orange, Blue, Green, White which are mapped to the 'Highest Priority' field.
I want a trigger to run on Contact object, after insert and update and check the value of 'Highest Priority' field on Contact. If the field value is 1, the contact should be added as a Campaign member in 'Red' Campaign. Similarly, if the highest priority is 2, the contact should be added to the 'Orange' Campaign and so on.... Also if the contact already exists in any of those campaigns and the 'Highest Priority' field value changes, the contact should be deleted from the current campaign and added as a campaign member in the appropriate campaign/bucket based on its mapping.
I have written the trigger below, checked the debug log, it does print that it has been added to the campaign but when I go and check the campaign, nothing shows up.Also, I don't know how to incorporate the condition where it deletes the contact from the current campaign and add it to the appropriate campaign.
trigger AddContacttoCampaign on Contact (after insert,after update) {
/*List<Contact> contactsWithHighestPriority =
[SELECT Id, Name, Highest_Priority__c FROM Contact WHERE Id IN :Trigger.New];*/
Campaign Red= [select id, name FROM Campaign where name =: 'Red'];
Campaign Orange= [select id, name FROM Campaign where name =: 'Orange'];
Campaign Blue= [select id, name FROM Campaign where name =: 'Blue'];
Campaign Green= [select id, name FROM Campaign where name =: 'Green'];
Campaign White= [select id, name FROM Campaign where name =: 'White'];
list<CampaignMember> CampaignMembersList = new list<CampaignMember>();
list<CampaignMember> CampaignMembersToBeDeleted = new list<CampaignMember>();
CampaignMembersToBeDeleted= [select id FROM CampaignMember where ContactId IN : Trigger.newMap.keySet() AND (CampaignId =: Red.Id OR
CampaignId =: Orange.Id OR CampaignId =: Blue.Id OR CampaignId =: Green.Id OR CampaignId =: White.Id)];
delete CampaignMembersToBeDeleted;
for(Contact c: Trigger.New) {
if(c.Highest_Priority__c == 1)
{
CampaignMember cm= New CampaignMember(CampaignId=Red.Id, ContactId= c.Id);
CampaignMembersList.add(cm);
System.debug('Contact added to Red Campaign');
}
if(c.Highest_Priority__c == 2)
{
CampaignMember cm= New CampaignMember(CampaignId=Orange.Id, ContactId= c.Id);
CampaignMembersList.add(cm);
System.debug('Contact added to Orange Campaign');
}
if(c.Highest_Priority__c == 3)
{
CampaignMember cm= New CampaignMember(CampaignId=Blue.Id, ContactId= c.Id);
CampaignMembersList.add(cm);
System.debug('Contact added to Blue Campaign');
}
if(c.Highest_Priority__c == 4)
{
CampaignMember cm= New CampaignMember(CampaignId=Green.Id, ContactId= c.Id);
CampaignMembersList.add(cm);
System.debug('Contact added to Green Campaign');
}
if(c.Highest_Priority__c == 5)
{
CampaignMember cm= New CampaignMember(CampaignId=White.Id, ContactId= c.Id);
CampaignMembersList.add(cm);
System.debug('Contact added to White Campaign');
}
}
insert CampaignMembersList;
}