You need to sign in to do that
Don't have an account?
Ryan Coe
Apex Trigger Removal or Modification
Hello-
Yesterday, I installed an apex trigger titled 'Change Lead Status' with the intent that anytime a Lead has a completed activity logged, it changes the Lead Status to 'Working - Contact Made'. While that functionality works just fine, my users have noticed an error message whenever they try and log an activity (manually) on an Account without a Contact included on the Name field in the Task page layout. The error will not let them log the Task without a Contact associated with it. I don't understand what is happening, and I'm confused as to why a Lead based apex trigger is affecting Accounts/Contacts.
I have two questions A) is there any way to modify this code so that this data is not required to log a Task manually? The apex trigger works fine other than this error. B) If not, how would I go about removing this apex trigger? I looked at some documentation around this, and they direct me to the sandbox to remove the trigger, but the trigger does not appear in my sandbox.
Here is the code:
trigger changeLeadStatus on Task (before insert, before update) {
String desiredNewLeadStatus = 'Working - Contacted';
List<Id> leadIds=new List<Id>();
for(Task t:trigger.new){
if(t.Status=='Completed'){
if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
leadIds.add(t.whoId);
}//if 2
}//if 1
}//for
List<Lead> leadsToUpdate=[SELECT Id, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE];
For (Lead l:leadsToUpdate){
l.Status=desiredNewLeadStatus;
}//for
try{
update leadsToUpdate;
}catch(DMLException e){
system.debug('Leads were not all properly updated. Error: '+e);
}
}//trigger
Any help would be greatly appreciated, I have received several complaints over the past day.
Thank you,
Ryan
The code is as follows:
Yesterday, I installed an apex trigger titled 'Change Lead Status' with the intent that anytime a Lead has a completed activity logged, it changes the Lead Status to 'Working - Contact Made'. While that functionality works just fine, my users have noticed an error message whenever they try and log an activity (manually) on an Account without a Contact included on the Name field in the Task page layout. The error will not let them log the Task without a Contact associated with it. I don't understand what is happening, and I'm confused as to why a Lead based apex trigger is affecting Accounts/Contacts.
I have two questions A) is there any way to modify this code so that this data is not required to log a Task manually? The apex trigger works fine other than this error. B) If not, how would I go about removing this apex trigger? I looked at some documentation around this, and they direct me to the sandbox to remove the trigger, but the trigger does not appear in my sandbox.
Here is the code:
trigger changeLeadStatus on Task (before insert, before update) {
String desiredNewLeadStatus = 'Working - Contacted';
List<Id> leadIds=new List<Id>();
for(Task t:trigger.new){
if(t.Status=='Completed'){
if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
leadIds.add(t.whoId);
}//if 2
}//if 1
}//for
List<Lead> leadsToUpdate=[SELECT Id, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE];
For (Lead l:leadsToUpdate){
l.Status=desiredNewLeadStatus;
}//for
try{
update leadsToUpdate;
}catch(DMLException e){
system.debug('Leads were not all properly updated. Error: '+e);
}
}//trigger
Any help would be greatly appreciated, I have received several complaints over the past day.
Thank you,
Ryan
The code is as follows:
Use below code
While you are looping through your tasks, make sure the WhoId is not null and is a Lead.
Also, before you insert leadsToUpdate, verify that there are leads in that list. If you try to insert an empty list, you will also get an error.
Thank you for all of your help!
I am in the sandbox now, so I am trying to modify the code.
Per your recommendation, I tried placing this in line 7: if(t.whoId != Lead && String.valueOf(t.whoId).startsWith('00Q')==TRUE){
But I am getting an error: Error: Compile Error: Variable does not exist: Lead at line 7 column 27
Do you know what I am doing wrong here?
Again, thank you for your help.
Thanks,
Ryan
to