You need to sign in to do that
Don't have an account?
How to create a trigger that will add a contact into a specific campaign based on pick list value?
Hi,
Please can someone show me how to create a trigger that will initiates an action to add or remove a contact into a specific campagin based on picklist field value on contact object.
Picklist Field Name: Add/Remove from Monthly Mailing list
Values: Add, Remove
Campaign Name: SCM World Monthly Mailing List
Logic:
If picklist value selected = "Add" , then add contact to campagin "SCM World Monthly Mailing List"
If picklist value selected = "Remove" , then remove contact from campaign "SCM World Monthly Mailing List"
Please ask questions if it needs further clarification.
Thanks,
Swapnil
The basic idea would be to first query for the campaign to which you would like to add/remove contacts. Then, you would iterate through the contacts affected by the trigger, checking if the picklist value had changed, and, if so, what value it had changed to. If the value changes to Add, then you'll create a CampaignMember object containing the campaign ID and the contact ID, and add it to a list of CampaignMembers to insert, after you are done iterating through all of the contacts. You'll add the contacts to Remove to a different list. It should look something like this (untested):
There are many ways to do it depends what type of relation ship you want to build between Contact anc Campaign.
1. if many to one means ( many contacts may get connected with single campaign) then we need to populate/clear lookup field on contact object -record based on selected picklist.
2. if Many to many ( means more than one contact may be associated with more then one campaign) then we need to create junction object between both of them.
Exmaple 2nd is exlained in above reply in beautiful way ( I appreciate it)
Example 1st would like below
Trigger Contact_connection_Campaign on contact(before update)
{
String CampaingID ;
// getting Campaing ID
for( Campaign camp : [select id from Campaign where name ='' limit 1])
{
CampaignID = camp.id ;
}
// connecting/Disconnecting Contact with Campaing
for( Contact c : Trigger.New)
{
if( c.PcikListValue__c == 'Add')
{
c.RelatedCampaign__c = campaignID ;
}
else if (c.PcikListValue__c == 'Remove')
{
c.RelatedCampaign__c = null ;
}
}
}
Just to re-open this question- I tried implementnig the trigger suggested by Cheyne but it gave me following error:
Error: Compile Error: Variable does not exist: membersToInsert at line 35 column 8
Had to modify the code a bit to suit my need:
trigger triggerContactCreateCampMember on Contact (after insert, after update) {
//Get the campaign ID
String campaignId = [SELECT Id FROM Campaign WHERE Name = 'Test' LIMIT 1].Id;
//Create a list to which we can add campaign member objects to insert
List<CampaignMember> membersToAdd = new List<CampaignMember>();
//Create a list of IDs of contacts which need to be removed from the campaign
List<String> contactIdsToDelete = new List<String>();
for (Contact c: Trigger.new) {
//Check if the field value has changed
if (Trigger.oldMap.get(c.Id).Member_Tier_Type__c !=
Trigger.newMap.get(c.Id).Member_Tier_Type__c) {
if (Contact.Member_Tier_Type__c == 'C-Level') {
//Create the campaign member and add it to the list of campaign members to update
CampaignMember cm = new CampaignMember(CampaignId=campaignId, ContactId=c.Id);
membersToAdd.add(cm);
} else if (c.Member_Tier_Type__c == 'Remove') {
//Add to list of contacts to remove from campaign
contactIdsToDelete.add(c.Id);
}
}
}
//Get the campaign member objects that need to be removed
List<CampaignMember> membersToDelete = [SELECT Id FROM CampaignMember WHERE ContactId IN
:contactIdsToDelete AND CampaignId = :campaignId];
delete membersToDelete;
insert membersToInsert;
}