+ Start a Discussion
Heshan Weerasekera 5Heshan Weerasekera 5 

trigger - Campaign Member

Hi - I am very new to dev ( like 1 week :) )... i need to try and write a trigger calculating the unique number of campaign member companies attached to a campaign. For example a campaign can have 50 members representing 10 companies. I need that 10. Would anyone have a similar trigger they can share which I will try and edit ? many many thanks 
Best Answer chosen by Heshan Weerasekera 5
HARSHIL U PARIKHHARSHIL U PARIKH
Hi Heshan,

This is possible via an Apex Trigger. I am also pretty positive that this cannot be achieve with a trigger.

You need to have a trigger on a CAMPAIGNMEMBER object in salesforce.

I would recommed doing following,

1) Create a field on CampaignMember object named Contact_s_AccountID__c which would pull contact's Account's ID. Here is a formula for it.
Contact.AccountId
2) Create a field on Campaign object which tracks the different Account number named Total_Different_Companies__c It's a number field with no decimal.
3) Create a trigger on campaign member object. Below is a trigger code you can use. This trigger would work in all conditions such us Insert, Update, Delete, Undelete etc.. If you are adding one of your campaign member who belongs to a new account then trigger will update Total_Different_Companies__c appropriately. Same thing goes for deleting same record etc..

Trigger Code:
Trigger TotalDifferentCompaniesAttached on CampaignMember (After Insert, After Update, After Delete, After UnDelete) {
	
    List<Id> campaignIds = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(CampaignMember member : Trigger.New){
            If(member.CampaignId != NULL && member.ContactId != NULL && member.Contact_s_AccountID__c != NULL){
                campaignIds.add(member.CampaignId);
            }
        }
    }
    
    If(Trigger.IsDelete){
        For(CampaignMember member : Trigger.Old){
            If(member.CampaignId != NULL && member.ContactId != NULL && member.Contact_s_AccountID__c != NULL){
                campaignIds.add(member.CampaignId);
            }
        }
    }
    
    List<Campaign> campaignListToUpdate = New List<Campaign>();
    
    For(Campaign camp : [Select Id, Total_Different_Companies__c, 
                         		(Select Id, Contact_s_AccountID__c FROM CampaignMembers 
                                 				WHERE CampaignId != NULL 
                                 				AND   ContactId != NULL 
                                 				AND   Contact_s_AccountID__c != NULL) 
                        									FROM Campaign WHERE Id =: campaignIds])
    {
        camp.Total_Different_Companies__c = 0;
        Set<String> uniqueIds = New Set<String>();
        
        For(CampaignMember EveryMember : camp.CampaignMembers)
        {
            uniqueIds.add(EveryMember.Contact_s_AccountID__c);
        }
        camp.Total_Different_Companies__c = uniqueIds.size();
        campaignListToUpdate.add(camp);
    }
    
    try{
        If(!campaignListToUpdate.isEmpty()){
            update campaignListToUpdate;
        }
    }
    Catch(Exception e){
        system.debug('Thrown Exception for TotalDifferentCompaniesAttached Is: ' + e.getMessage());
    }
}

If you would like to see the visual representation then here we go,

User-added image


Heshan, if this answers your question then please mark it as Best Answer by clicking best anwer link!
Thanks.

All Answers

HARSHIL U PARIKHHARSHIL U PARIKH
Hi Heshan,

This is possible via an Apex Trigger. I am also pretty positive that this cannot be achieve with a trigger.

You need to have a trigger on a CAMPAIGNMEMBER object in salesforce.

I would recommed doing following,

1) Create a field on CampaignMember object named Contact_s_AccountID__c which would pull contact's Account's ID. Here is a formula for it.
Contact.AccountId
2) Create a field on Campaign object which tracks the different Account number named Total_Different_Companies__c It's a number field with no decimal.
3) Create a trigger on campaign member object. Below is a trigger code you can use. This trigger would work in all conditions such us Insert, Update, Delete, Undelete etc.. If you are adding one of your campaign member who belongs to a new account then trigger will update Total_Different_Companies__c appropriately. Same thing goes for deleting same record etc..

Trigger Code:
Trigger TotalDifferentCompaniesAttached on CampaignMember (After Insert, After Update, After Delete, After UnDelete) {
	
    List<Id> campaignIds = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(CampaignMember member : Trigger.New){
            If(member.CampaignId != NULL && member.ContactId != NULL && member.Contact_s_AccountID__c != NULL){
                campaignIds.add(member.CampaignId);
            }
        }
    }
    
    If(Trigger.IsDelete){
        For(CampaignMember member : Trigger.Old){
            If(member.CampaignId != NULL && member.ContactId != NULL && member.Contact_s_AccountID__c != NULL){
                campaignIds.add(member.CampaignId);
            }
        }
    }
    
    List<Campaign> campaignListToUpdate = New List<Campaign>();
    
    For(Campaign camp : [Select Id, Total_Different_Companies__c, 
                         		(Select Id, Contact_s_AccountID__c FROM CampaignMembers 
                                 				WHERE CampaignId != NULL 
                                 				AND   ContactId != NULL 
                                 				AND   Contact_s_AccountID__c != NULL) 
                        									FROM Campaign WHERE Id =: campaignIds])
    {
        camp.Total_Different_Companies__c = 0;
        Set<String> uniqueIds = New Set<String>();
        
        For(CampaignMember EveryMember : camp.CampaignMembers)
        {
            uniqueIds.add(EveryMember.Contact_s_AccountID__c);
        }
        camp.Total_Different_Companies__c = uniqueIds.size();
        campaignListToUpdate.add(camp);
    }
    
    try{
        If(!campaignListToUpdate.isEmpty()){
            update campaignListToUpdate;
        }
    }
    Catch(Exception e){
        system.debug('Thrown Exception for TotalDifferentCompaniesAttached Is: ' + e.getMessage());
    }
}

If you would like to see the visual representation then here we go,

User-added image


Heshan, if this answers your question then please mark it as Best Answer by clicking best anwer link!
Thanks.
This was selected as the best answer
Heshan Weerasekera 5Heshan Weerasekera 5
thank you so very much... ur legand!!!!