-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
4Questions
-
6Replies
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() );
}
}
- Nick Mirams
- December 08, 2014
- Like
- 0
- Continue reading or reply
How do I select a product from a picklist in leads and add it to the opportunity thats been created upon lead conversion
Hi,
I am struggling with this one. I have tried a few ways but am very new to apex code and not sure it can be done wthout coding.
I have the users select products at the lead stage from a picklist. I want to be able to copy these products when the lead is converted and add them to the newly created opportunity.I know these is difficult as each product is associated to price books but can it be done?
Nick
I am struggling with this one. I have tried a few ways but am very new to apex code and not sure it can be done wthout coding.
I have the users select products at the lead stage from a picklist. I want to be able to copy these products when the lead is converted and add them to the newly created opportunity.I know these is difficult as each product is associated to price books but can it be done?
Nick
- Nick Mirams
- November 18, 2014
- Like
- 0
- Continue reading or reply
Trigger to update the task status after Lead Status is 'Closed Lost' or 'Qualified'
HI,
I am getting this error, can anyone help?: -
System.NullPointerException: Attempt to de-reference a null object: Trigger.NickMiramsTaskLeadUpdate: line 30, column 1
heres the code...I am trying to write a trigger to close a task as 'Completed' after a lead status becomes Closed Lost or Qualified...
trigger NickMiramsTaskLeadUpdate on Lead (after update){
set<id> leadids = new set<id>();
map<id,list<task>> leadtotaskmap = new map<id,list<task>>();
list<task> taskstoupdate = new list<task>();
for(lead Lead : trigger.new){
leadids.add(lead.id);
}
list<task> tasklist = [select whatid,status from task where whatid in : leadids];
for(task t : tasklist){
if(leadtotaskmap.get(t.whatid)!=null){
list<task> temp = leadtotaskmap.get(t.whatid);
temp.add(t);
leadtotaskmap.put(t.whatid, temp);
}
else
{
leadtotaskmap.put(t.whatid, new list<task>{t});
}
}
for(lead lead : trigger.new){
if(lead.status=='Closed Lost' || lead.status=='Qualified'){
list<Task> tasklisttemp = leadtotaskmap.get(lead.id);
for(task t : tasklisttemp){
t.status='completed';
taskstoupdate.add(t);
}
}
}
if(taskstoupdate.size()>0)
update taskstoupdate;
}
I am getting this error, can anyone help?: -
System.NullPointerException: Attempt to de-reference a null object: Trigger.NickMiramsTaskLeadUpdate: line 30, column 1
heres the code...I am trying to write a trigger to close a task as 'Completed' after a lead status becomes Closed Lost or Qualified...
trigger NickMiramsTaskLeadUpdate on Lead (after update){
set<id> leadids = new set<id>();
map<id,list<task>> leadtotaskmap = new map<id,list<task>>();
list<task> taskstoupdate = new list<task>();
for(lead Lead : trigger.new){
leadids.add(lead.id);
}
list<task> tasklist = [select whatid,status from task where whatid in : leadids];
for(task t : tasklist){
if(leadtotaskmap.get(t.whatid)!=null){
list<task> temp = leadtotaskmap.get(t.whatid);
temp.add(t);
leadtotaskmap.put(t.whatid, temp);
}
else
{
leadtotaskmap.put(t.whatid, new list<task>{t});
}
}
for(lead lead : trigger.new){
if(lead.status=='Closed Lost' || lead.status=='Qualified'){
list<Task> tasklisttemp = leadtotaskmap.get(lead.id);
for(task t : tasklisttemp){
t.status='completed';
taskstoupdate.add(t);
}
}
}
if(taskstoupdate.size()>0)
update taskstoupdate;
}
- Nick Mirams
- October 06, 2014
- Like
- 0
- Continue reading or reply
Trigger to update the task status after opportunity status is won or lost
Hi,
Can someone help as i am pretty new to all this...
I want to write a trigger on the task which updates the task status to completed after the oipportunity is set to Closed Won or Closed lost.
This is to basically prevent any closed opportunities which still have open tasks.
Its an if statement of some sort but I need to learn how to do all this
Thanks in Advanced.
Nick
Can someone help as i am pretty new to all this...
I want to write a trigger on the task which updates the task status to completed after the oipportunity is set to Closed Won or Closed lost.
This is to basically prevent any closed opportunities which still have open tasks.
Its an if statement of some sort but I need to learn how to do all this
Thanks in Advanced.
Nick
- Nick Mirams
- October 03, 2014
- Like
- 0
- Continue reading or reply
How do I select a product from a picklist in leads and add it to the opportunity thats been created upon lead conversion
Hi,
I am struggling with this one. I have tried a few ways but am very new to apex code and not sure it can be done wthout coding.
I have the users select products at the lead stage from a picklist. I want to be able to copy these products when the lead is converted and add them to the newly created opportunity.I know these is difficult as each product is associated to price books but can it be done?
Nick
I am struggling with this one. I have tried a few ways but am very new to apex code and not sure it can be done wthout coding.
I have the users select products at the lead stage from a picklist. I want to be able to copy these products when the lead is converted and add them to the newly created opportunity.I know these is difficult as each product is associated to price books but can it be done?
Nick
- Nick Mirams
- November 18, 2014
- Like
- 0
- Continue reading or reply
Trigger to update the task status after opportunity status is won or lost
Hi,
Can someone help as i am pretty new to all this...
I want to write a trigger on the task which updates the task status to completed after the oipportunity is set to Closed Won or Closed lost.
This is to basically prevent any closed opportunities which still have open tasks.
Its an if statement of some sort but I need to learn how to do all this
Thanks in Advanced.
Nick
Can someone help as i am pretty new to all this...
I want to write a trigger on the task which updates the task status to completed after the oipportunity is set to Closed Won or Closed lost.
This is to basically prevent any closed opportunities which still have open tasks.
Its an if statement of some sort but I need to learn how to do all this
Thanks in Advanced.
Nick
- Nick Mirams
- October 03, 2014
- Like
- 0
- Continue reading or reply