0

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;


 }

1 Answer 1

2

This is really two questions, but one of them is very simple, so let's get that out of the way quickly.

it does print that it has been added to the campaign but when I go and check the campaign, nothing shows up

That's because you don't perform an insert DML to persist your CampaignMember records to the database. You'll want to do this in a bulkified way, so you'll accumulate a List<CampaignMember>, to which you add each record as you create it, and insert that list outside your Contact loop.

Also, I don't know how to incorporate the condition where it deletes the contact from the current campaign

Well, I'll try to get you started on the logic for this piece, and let you finish the implementation. You want your Contacts to be in these Campaigns exactly once - each Contact gets to be in one and only one Campaign. Your current code always creates a record for each Contact in the trigger set. (This can be optimized later, but let's deal with the logic first).

So you know, when you get those Contacts, that you need to delete any CampaignMember that has a ContactId in Trigger.newMap.keySet(), and that has a CampaignId that's one of the Ids of those five Campaigns you queried.

So you'll want a delete DML, prior to your Contact loop, and you'll want to run that delete against a query to identify CampaignMember records whose ContactId IN :Trigger.newMap.keySet() and whose CampaignId is one of your five Campaigns.

Does that get you started?

5
  • I was successful in completing the first part. Thanks for the help. I am now working on the second part. I have updated my question above. I need help with the query where I am fetching the list of all campaignmembers where the contact already exists. But it gives me some syntax error "Compile Error: Unexpected token '=' "
    – Student
    Commented Feb 11, 2019 at 18:57
  • You need to use : anywhere you are binding an Apex value, and you need to not use : anywhere you are using a literal value. You are on the right track!
    – David Reed
    Commented Feb 11, 2019 at 19:03
  • I am not sure if I am able to resolve the error.
    – Student
    Commented Feb 11, 2019 at 20:02
  • Actually I resolved it :) I was using ] before ). Here is the updated query 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)]; The contact is actually getting deleted from the old campaign and added into new campaign now :) Thanks alot... I am updating my question to include the answer for others.
    – Student
    Commented Feb 11, 2019 at 20:14
  • Yep, you got it. Well done.
    – David Reed
    Commented Feb 11, 2019 at 20:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .