+ Start a Discussion
cpo87cpo87 

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

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

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

souvik9086souvik9086

Just a bracket end is missing in the last if.Try below code.

 

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; } } }

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

cpo87cpo87

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.

souvik9086souvik9086

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

cpo87cpo87

It's saying that the variable 'lead' doesn't exist in this line:       leadToBeUpdated.add(lead);

souvik9086souvik9086

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

This was selected as the best answer
cpo87cpo87

That works!  Thanks.  Now I just have to figure out how to only make the update if Primary_Campaign_Source_Lead__c is Null.

souvik9086souvik9086

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.

cpo87cpo87

Thanks for the reply, unfortunately I tried it that way as well but it is still updating when the field isn't null.

souvik9086souvik9086

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

cpo87cpo87

That works!  Thanks again!