function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Swapnil PatneSwapnil Patne 

How to create a trigger that will add a contact into a specific campaign based on two pick list value?

Please can someone help me with how to create a trigger that will add or remove a contact into a specific campagin based on two picklist fields, one on contact object and second on account obbject.

If contact is tagged as CFL or C-Level and Account is active (yes) then add the contact to specific campaign. If account is not active ( = No) then do not add.
If contact is modified and untagged as CFL or C-Level and is a member of the campaign then remove contact from campaign.

If contact is 

Contact picklist field name: Member Tier Type 
Values: CFL, C-Level

Account pick list field name:  Active
Values: Yes, No 

Add contacts to campaign:
Campaign ID = 70111000000GTNQ

I hope someone can help.

Thanks,
Swapnil
ShashankShashank (Salesforce Developers) 
Here's a sample trigger:
 
trigger addContactToCampaign on Contact (after insert, after update) {
    set<Id> accIds = new set<Id>();
    list<campaignmember> cmlist = new list<campaignmember>();
    for(contact c:trigger.new){
        accIds.add(c.accountId);
    }
    map<Id,account> accmap = new map<id,account>([select Id,active__c from account where Id in :accIds]);
    for(contact co:trigger.new){
        if((co.member_tier_type__c == 'CFL')||(co.member_tier_type__c == 'C-Level')){
            if(accmap.get(co.accountId).active__c == 'Yes'){
                campaignmember cm = new campaignmember();
                cm.campaignId = '70111000000GTNQ';
                cm.contactId = co.Id;
                cmlist.add(cm);
            }
        }
    }
    if(cmlist.size()>0) insert cmlist;
}