You need to sign in to do that
Don't have an account?
cpo87
Update a custom campaign lookup field on leads if null with the campaign ID from Campaign Member
Hello all,
I would like to populate a custom campaign lookup field on leads using a trigger. This trigger should look at each new Campaign Member created, check to see if this field on the associated Lead is null and if so populate the campaign ID from the Campaign Member record.
I've created the below code but i can't figure out what I'm doing wrong. My current error is saying I have an unexpected token '{' after my last IF statement.
I haven't gotten to the check if the field is Null yet.
trigger trgCM_PopulatePrimaryCampaign on CampaignMember (before insert) { //create and populate a list of Campaign Member Lead IDs List<ID> cmLeadID = new List<ID>(); for(CampaignMember cm:Trigger.new){ cmLeadID.add(cm.LeadID); }
//create a list of leads that have Lead Ids in cmLeadID List<Lead> leads = [ SELECT Id, Primary_Campaign_Source_Lead__c FROM Lead WHERE ID IN :cmLeadID ];
//create and populate a set of lead IDs using the leads list Set<ID> leadIDs = new Set<ID>(); for(Lead lead:leads){ leadIDs.add(lead.ID); } //check to see if Primary_Campaign_Source_Lead__c is null and if so populate with the cm.CampaignID
for(CampaignMember cm:Trigger.new){ if(leadIDs.contains(cm.LeadID){ lead.Primary_Campaign_Source_Lead__c = cm.CampaignID; } } }
Can anyone steer me in the right direction?
Thanks!
Christian
Ok, Do like this
trigger trgCM_PopulatePrimaryCampaign on CampaignMember (before insert) {
//create and populate a list of Campaign Member Lead IDs
List<lead> leadToBeUpdated = new list<Lead>();
List<ID> cmLeadID = new List<ID>();
for(CampaignMember cm:Trigger.new){
cmLeadID.add(cm.LeadID);
}
//create a list of leads that have Lead Ids in cmLeadID
List<Lead> leads = [
SELECT
Id, Primary_Campaign_Source_Lead__c
FROM
Lead
WHERE
ID IN :cmLeadID
];
//create and populate a set of lead IDs using the leads list
Set<ID> leadIDs = new Set<ID>();
for(Lead lead:leads){
leadIDs.add(lead.ID);
}
//check to see if Primary_Campaign_Source_Lead__c is null and if so populate with the cm.CampaignID
for(CampaignMember cm:Trigger.new){
if(leadIDs.contains(cm.LeadID)){
Lead lead = new Lead(Id=cm.LeadID);
lead.Primary_Campaign_Source_Lead__c = cm.CampaignID;
leadToBeUpdated.add(lead);
}
}
if(leadToBeUpdated.size()>0){
upsert leadToBeUpdated;
}
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
All Answers
Just a bracket end is missing in the last if.Try below code.
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
Ah, missed that one. Thanks. The follow up error says "Save error: Expression cannot be assigned". This is on line 0, any idea what that means? I've been researching it to no avail, I'm afraid my code has a serious logic error in it.
Try this
You have to update the lead record. Expression cannot be assigned.
trigger trgCM_PopulatePrimaryCampaign on CampaignMember (before insert) {
//create and populate a list of Campaign Member Lead IDs
List<lead> leadToBeUpdated = new list<Lead>();
List<ID> cmLeadID = new List<ID>();
for(CampaignMember cm:Trigger.new){
cmLeadID.add(cm.LeadID);
}
//create a list of leads that have Lead Ids in cmLeadID
List<Lead> leads = [
SELECT
Id, Primary_Campaign_Source_Lead__c
FROM
Lead
WHERE
ID IN :cmLeadID
];
//create and populate a set of lead IDs using the leads list
Set<ID> leadIDs = new Set<ID>();
for(Lead lead:leads){
leadIDs.add(lead.ID);
}
//check to see if Primary_Campaign_Source_Lead__c is null and if so populate with the cm.CampaignID
for(CampaignMember cm:Trigger.new){
if(leadIDs.contains(cm.LeadID)){
lead.Primary_Campaign_Source_Lead__c = cm.CampaignID;
leadToBeUpdated.add(lead);
}
}
if(leadToBeUpdated.size()>0){
upsert leadToBeUpdated;
}
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
It's saying that the variable 'lead' doesn't exist in this line: leadToBeUpdated.add(lead);
Ok, Do like this
trigger trgCM_PopulatePrimaryCampaign on CampaignMember (before insert) {
//create and populate a list of Campaign Member Lead IDs
List<lead> leadToBeUpdated = new list<Lead>();
List<ID> cmLeadID = new List<ID>();
for(CampaignMember cm:Trigger.new){
cmLeadID.add(cm.LeadID);
}
//create a list of leads that have Lead Ids in cmLeadID
List<Lead> leads = [
SELECT
Id, Primary_Campaign_Source_Lead__c
FROM
Lead
WHERE
ID IN :cmLeadID
];
//create and populate a set of lead IDs using the leads list
Set<ID> leadIDs = new Set<ID>();
for(Lead lead:leads){
leadIDs.add(lead.ID);
}
//check to see if Primary_Campaign_Source_Lead__c is null and if so populate with the cm.CampaignID
for(CampaignMember cm:Trigger.new){
if(leadIDs.contains(cm.LeadID)){
Lead lead = new Lead(Id=cm.LeadID);
lead.Primary_Campaign_Source_Lead__c = cm.CampaignID;
leadToBeUpdated.add(lead);
}
}
if(leadToBeUpdated.size()>0){
upsert leadToBeUpdated;
}
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
That works! Thanks. Now I just have to figure out how to only make the update if Primary_Campaign_Source_Lead__c is Null.
trigger trgCM_PopulatePrimaryCampaign on CampaignMember (before insert) {
//create and populate a list of Campaign Member Lead IDs
List<lead> leadToBeUpdated = new list<Lead>();
List<ID> cmLeadID = new List<ID>();
for(CampaignMember cm:Trigger.new){
cmLeadID.add(cm.LeadID);
}
//create a list of leads that have Lead Ids in cmLeadID
List<Lead> leads = [
SELECT
Id, Primary_Campaign_Source_Lead__c
FROM
Lead
WHERE
ID IN :cmLeadID
];
//create and populate a set of lead IDs using the leads list
Set<ID> leadIDs = new Set<ID>();
for(Lead lead:leads){
leadIDs.add(lead.ID);
}
//check to see if Primary_Campaign_Source_Lead__c is null and if so populate with the cm.CampaignID
for(CampaignMember cm:Trigger.new){
if(leadIDs.contains(cm.LeadID)){
Lead lead = new Lead(Id=cm.LeadID);
if(lead.Primary_Campaign_Source_Lead__c == NULL){
lead.Primary_Campaign_Source_Lead__c = cm.CampaignID;
leadToBeUpdated.add(lead);
}
}
}
if(leadToBeUpdated.size()>0){
upsert leadToBeUpdated;
}
}
If the post is helpful please throw KUDOS.
Thanks for the reply, unfortunately I tried it that way as well but it is still updating when the field isn't null.
Now try this. this is supposed to work.
trigger trgCM_PopulatePrimaryCampaign on CampaignMember (before insert) {
//create and populate a list of Campaign Member Lead IDs
List<lead> leadToBeUpdated = new list<Lead>();
List<ID> cmLeadID = new List<ID>();
for(CampaignMember cm:Trigger.new){
cmLeadID.add(cm.LeadID);
}
//create a list of leads that have Lead Ids in cmLeadID
List<Lead> leads = [
SELECT
Id, Primary_Campaign_Source_Lead__c
FROM
Lead
WHERE
ID IN :cmLeadID
];
//create and populate a set of lead IDs using the leads list
Set<ID> leadIDs = new Set<ID>();
for(Lead lead:leads){
leadIDs.add(lead.ID);
}
//check to see if Primary_Campaign_Source_Lead__c is null and if so populate with the cm.CampaignID
for(CampaignMember cm:Trigger.new){
if(leadIDs.contains(cm.LeadID)){
Lead lead = [select id,name,Primary_Campaign_Source_Lead__c from Lead where id = :cm.LeadID];
if(lead.Primary_Campaign_Source_Lead__c == NULL){
lead.Primary_Campaign_Source_Lead__c = cm.CampaignID;
leadToBeUpdated.add(lead);
}
}
}
if(leadToBeUpdated.size()>0){
upsert leadToBeUpdated;
}
}
If the post helps you please throw KUDOS.
Thanks
That works! Thanks again!