You need to sign in to do that
Don't have an account?
Gtemp
Apex trigger to generate campaign member list
I was thinking about how to create a trigger that reads through all campaign members of a certain campaign and updates a custom campaign text field with a string containing the names of all members.
For example, in campaign "A" there are 3 members whose names are "John", "Joe" and "Jack". A custom field Member_List__c on "A" would read (after the trigger is executed) "John, Joe, Jack". (In my case, campaigns have only a small number of members so a default text field is fine, no need for long text areas)
Do you have any ideas?
Thank you!!
Hi
Here is the code you can use it. In the code i have used on formula field for getting name of lead or contact. U need to crate a formula field like this
Field text , field Name Name_1__c.
if(ContactId != null, Contact.FirstName , Lead.FirstName )
If u dont need this field u can map to your own field
trigger CampaignMembertrigger on CampaignMember (after insert) {
/*
In the trigger when the campaigns members are created/update iterate over trigger.new,
Take a map of campaigns id, Campaign and add values to map, After adding in map.
Query the campaigns where id in setofkey.
then iterate over the map and update the campaigns.
*/
map<Id, Campaign> map_CampaignId_Campaign = new map<Id, Campaign>();
Campaign locCampaign;
for(CampaignMember campaignMember : trigger.new) {
if(map_CampaignId_Campaign.containsKey(campaignMember.CampaignId)) {
locCampaign = new Campaign(Id = campaignMember.CampaignId);
locCampaign = map_CampaignId_Campaign.get(campaignMember.CampaignId);
locCampaign.Member_List__c = locCampaign.Member_List__c + ',' + campaignMember.Name_1__c;
map_CampaignId_Campaign.put(campaignMember.CampaignId,locCampaign);
} else {
locCampaign = new Campaign(Id = campaignMember.CampaignId , Member_List__c = campaignMember.Name_1__c);
map_CampaignId_Campaign.put(campaignMember.CampaignId,locCampaign);
}
}
map<Id, Campaign> map_CampaignId_CampaignAlreadyPrsent = new map<Id, Campaign>([Select Member_List__c
FROM Campaign
WHERE ID in: map_CampaignId_Campaign.KeySet()
AND Member_List__c != null]);
for(Id campaignId : map_CampaignId_Campaign.KeySet()) {
if(map_CampaignId_CampaignAlreadyPrsent.containsKey(campaignId)) {
locCampaign = new Campaign(Id = CampaignId);
locCampaign = map_CampaignId_Campaign.get(campaignId);
locCampaign.Member_List__c = map_CampaignId_CampaignAlreadyPrsent.get(campaignId).Member_List__c + locCampaign.Member_List__c;
map_CampaignId_Campaign.put(campaignId , locCampaign);
}
}
if(!map_CampaignId_Campaign.isEmpty()) {
update map_CampaignId_Campaign.values();
}
}
All Answers
then iterate over the map and update the campaigns.
Hey,
What do you intend to do with this field once it's populated? If it's a text field delimited with commas then you won't get much use out of it from a workflow, formula or reporting perspecitve.
If you're looking to simply view this information have you thought about using a report? Or inserting a custom link which passes the campaign id into a report to filter the results?
Thanks
@Devender M Thank you. I am a complete newbie with respect to Apex, so I still have to figure out what all these terms mean. But thank you anyway for your help.
@Oliver Actually I need this because I have a custom lookup field on Campaigns that assigs a Lead to each campaign (you may see this a "main target" of the campaign). So I want the names of all members to appear in a related list in the Lead detail page. This solution was the easiest one that crossed my mind ...
Hi
Here is the code you can use it. In the code i have used on formula field for getting name of lead or contact. U need to crate a formula field like this
Field text , field Name Name_1__c.
if(ContactId != null, Contact.FirstName , Lead.FirstName )
If u dont need this field u can map to your own field
trigger CampaignMembertrigger on CampaignMember (after insert) {
/*
In the trigger when the campaigns members are created/update iterate over trigger.new,
Take a map of campaigns id, Campaign and add values to map, After adding in map.
Query the campaigns where id in setofkey.
then iterate over the map and update the campaigns.
*/
map<Id, Campaign> map_CampaignId_Campaign = new map<Id, Campaign>();
Campaign locCampaign;
for(CampaignMember campaignMember : trigger.new) {
if(map_CampaignId_Campaign.containsKey(campaignMember.CampaignId)) {
locCampaign = new Campaign(Id = campaignMember.CampaignId);
locCampaign = map_CampaignId_Campaign.get(campaignMember.CampaignId);
locCampaign.Member_List__c = locCampaign.Member_List__c + ',' + campaignMember.Name_1__c;
map_CampaignId_Campaign.put(campaignMember.CampaignId,locCampaign);
} else {
locCampaign = new Campaign(Id = campaignMember.CampaignId , Member_List__c = campaignMember.Name_1__c);
map_CampaignId_Campaign.put(campaignMember.CampaignId,locCampaign);
}
}
map<Id, Campaign> map_CampaignId_CampaignAlreadyPrsent = new map<Id, Campaign>([Select Member_List__c
FROM Campaign
WHERE ID in: map_CampaignId_Campaign.KeySet()
AND Member_List__c != null]);
for(Id campaignId : map_CampaignId_Campaign.KeySet()) {
if(map_CampaignId_CampaignAlreadyPrsent.containsKey(campaignId)) {
locCampaign = new Campaign(Id = CampaignId);
locCampaign = map_CampaignId_Campaign.get(campaignId);
locCampaign.Member_List__c = map_CampaignId_CampaignAlreadyPrsent.get(campaignId).Member_List__c + locCampaign.Member_List__c;
map_CampaignId_Campaign.put(campaignId , locCampaign);
}
}
if(!map_CampaignId_Campaign.isEmpty()) {
update map_CampaignId_Campaign.values();
}
}