You need to sign in to do that
Don't have an account?
Guru 91
Trigger Help on Campaign Primary source update?
Hi Everyone,
Basically what I need is to override the default behavior of SFDC with regards to setting the Primary Campaign Source in Opportunities. The default behavior is it takes the last campaign associated with a contact and make it the Primary Campaign Source in the Opportunity. WE DO NOT WANT THAT. What we want is the FIRST RESPONDED Campaign of the contact to be the Primary Campaign Source.
I written code above only takes the first associated campaign, we need to updated with first responded campaign.
Here is my code:
trigger SetPrimaryCampaignSource on Opportunity (before insert) {
Set<Id> contIds = new Set<Id>();
for (Opportunity opp : Trigger.new) {
contIds.add(opp.Sourced_by__c);
}
contIds.remove(null);
if (!contIds.isEmpty()) {
Map<Id, CampaignMember> campaignMap = new Map<Id, CampaignMember>();
for (CampaignMember cm : [
select ContactId,
CampaignId,
Status
from CampaignMember
where ContactId in :contIds
order by CreatedDate asc
nulls last
]) {
campaignMap.put(cm.ContactId, cm);
}
for (Opportunity opp : Trigger.new) {
if (campaignMap.containsKey(opp.Sourced_by__c)) {
opp.CampaignId = campaignMap.get(opp.Sourced_by__c).CampaignId;
}
}
}
}
Thanks
Basically what I need is to override the default behavior of SFDC with regards to setting the Primary Campaign Source in Opportunities. The default behavior is it takes the last campaign associated with a contact and make it the Primary Campaign Source in the Opportunity. WE DO NOT WANT THAT. What we want is the FIRST RESPONDED Campaign of the contact to be the Primary Campaign Source.
I written code above only takes the first associated campaign, we need to updated with first responded campaign.
Here is my code:
trigger SetPrimaryCampaignSource on Opportunity (before insert) {
Set<Id> contIds = new Set<Id>();
for (Opportunity opp : Trigger.new) {
contIds.add(opp.Sourced_by__c);
}
contIds.remove(null);
if (!contIds.isEmpty()) {
Map<Id, CampaignMember> campaignMap = new Map<Id, CampaignMember>();
for (CampaignMember cm : [
select ContactId,
CampaignId,
Status
from CampaignMember
where ContactId in :contIds
order by CreatedDate asc
nulls last
]) {
campaignMap.put(cm.ContactId, cm);
}
for (Opportunity opp : Trigger.new) {
if (campaignMap.containsKey(opp.Sourced_by__c)) {
opp.CampaignId = campaignMap.get(opp.Sourced_by__c).CampaignId;
}
}
}
}
Thanks
Try This :
Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Salesforce latest interview questions :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
But how to get the first reponded campaign from that contact , only the firstly responed contact. Example:- for 1 campaign member (Contact) 5 campaign are related , so to which campaign that contact responded first out of those all 5 that one i need to fetch and update in opportunity.