You need to sign in to do that
Don't have an account?
Nick Mirams
trigger to automatically update lead with campaign
Hi,
I am completely new to apex and trying to learn.
What I need to do it when a web-2-lead form is created and comes into salesforce, I need a trigger to add that lead to a campaign (the campaign names are the same as the lead sources).
The code worked in sandbox but not working in Production so not deploying due to code coverage.
Can someone help me learn whats wrong and understand more of this.
Code i have is: -
trigger Create_CampaignMember_For_New_Leads on Lead (after insert, after update) {
try {
if (Trigger.new.size() == 1) {
List <CampaignMember> cm = new list<CampaignMember>();
for(Lead L : Trigger.new) {
String cname = L.leadsource;
String replaceText2 = 'dup-';
cname = cname.replace(replaceText2,'');
List <Campaign> c = [select id, name from Campaign where name = :cname limit 1];
if(!c.isEmpty()){
CampaignMember cml = new CampaignMember();
cml.campaignid = c[0].id;
cml.leadid = l.id;
cm.add(cml);
}
}
if(!cm.isEmpty()){
insert cm;
}
}
} catch(Exception e) {
system.debug ('error: ' + e.getMessage() );
}
}
I am completely new to apex and trying to learn.
What I need to do it when a web-2-lead form is created and comes into salesforce, I need a trigger to add that lead to a campaign (the campaign names are the same as the lead sources).
The code worked in sandbox but not working in Production so not deploying due to code coverage.
Can someone help me learn whats wrong and understand more of this.
Code i have is: -
trigger Create_CampaignMember_For_New_Leads on Lead (after insert, after update) {
try {
if (Trigger.new.size() == 1) {
List <CampaignMember> cm = new list<CampaignMember>();
for(Lead L : Trigger.new) {
String cname = L.leadsource;
String replaceText2 = 'dup-';
cname = cname.replace(replaceText2,'');
List <Campaign> c = [select id, name from Campaign where name = :cname limit 1];
if(!c.isEmpty()){
CampaignMember cml = new CampaignMember();
cml.campaignid = c[0].id;
cml.leadid = l.id;
cm.add(cml);
}
}
if(!cm.isEmpty()){
insert cm;
}
}
} catch(Exception e) {
system.debug ('error: ' + e.getMessage() );
}
}
Shashank (Salesforce Developers)
So is your issue with the trigger or the code coverage for the trigger? The trigger code looks ok.