You need to sign in to do that
Don't have an account?
Wes McCarthy
Task trigger updating Lead record incorrectly.
Once i get the Task associated with my Lead record, i want to perform the following operations:
If Task is "A" and Status = Completed
Update Lead Status to 'Contacted.'
If Task is "B" and Status = Completed and Lead Description field is Null
Throw an error - 'Task cannot be completed while description is Null.
Else
Update Lead Status to 'Qualified'.
The issue i've encountered is that when i complete Task "A", the Lead Status directly updates to 'Qualified'. When i complete Task "B" and Lead Description is Null, nothing happens. I dont receive an error message. My code is below.
Any suggestions are very welcome.
Thank you.
trigger LeadTrigger on Task (before insert, before update) {
List<Id> leadIds=new List<Id>();
for(Task t:trigger.new) {
if(t.Status=='Completed') {
if(t.whoId != null && 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
map<id,Lead> LeadtaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE]);
for(Task tsk:trigger.new){
if(LeadtaskMap.keyset().contains(tsk.whoid)) {
if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead') {
LeadtaskMap.get(tsk.whoid).Status = 'Contacted';
}
if(LeadtaskMap.get(tsk.whoid).Description == null && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportuntity') {
tsk.adderror('Task cannot be completed until the following fields have been populated: Description');
}
Else {
LeadtaskMap.get(tsk.whoid).Status = 'Qualified';
}
}
}
List<Lead> leadList = LeadtaskMap.values();
update leadList;
}
If Task is "A" and Status = Completed
Update Lead Status to 'Contacted.'
If Task is "B" and Status = Completed and Lead Description field is Null
Throw an error - 'Task cannot be completed while description is Null.
Else
Update Lead Status to 'Qualified'.
The issue i've encountered is that when i complete Task "A", the Lead Status directly updates to 'Qualified'. When i complete Task "B" and Lead Description is Null, nothing happens. I dont receive an error message. My code is below.
Any suggestions are very welcome.
Thank you.
trigger LeadTrigger on Task (before insert, before update) {
List<Id> leadIds=new List<Id>();
for(Task t:trigger.new) {
if(t.Status=='Completed') {
if(t.whoId != null && 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
map<id,Lead> LeadtaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE]);
for(Task tsk:trigger.new){
if(LeadtaskMap.keyset().contains(tsk.whoid)) {
if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead') {
LeadtaskMap.get(tsk.whoid).Status = 'Contacted';
}
if(LeadtaskMap.get(tsk.whoid).Description == null && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportuntity') {
tsk.adderror('Task cannot be completed until the following fields have been populated: Description');
}
Else {
LeadtaskMap.get(tsk.whoid).Status = 'Qualified';
}
}
}
List<Lead> leadList = LeadtaskMap.values();
update leadList;
}
trigger LeadTrigger on Task (before insert, before update)
{
List<Id> leadIds=new List<Id>();
for(Task t:trigger.new)
{
if(t.Status=='Completed')
{
if(t.whoId != null && 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
map<id,Lead> LeadtaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE]);
for(Task tsk:trigger.new)
{
if(LeadtaskMap.keyset().contains(tsk.whoid))
{
if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead')
{
LeadtaskMap.get(tsk.whoid).Status = 'Contacted';
}
// System.debug('1 condition:' + LeadtaskMap.get(tsk.whoid).Description == NULL);
// System.debug('2 condition:' + tsk.Status == 'Completed');
// System.debug('3 condition:' + tsk.Subject == 'Make contact with Lead');
// System.debug('task:' + tsk);
Else if(LeadtaskMap.get(tsk.whoid).Description == Null && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Lead')
{
tsk.adderror('Task cannot be completed until the following fields have been populated: Description');
}
Else
{
LeadtaskMap.get(tsk.whoid).Status = 'Qualified';
}
}
}
List<Lead> leadList = LeadtaskMap.values();
update leadList;
}
All Answers
I saved this trigger in my dev org and tested when lead description = null and Status is 'Completed' and Subject 'Qualify Opportunity' and I got error message. Could you check if you select correct status and subject, also that Subject is spelled correctly. Also try to add System.debug before if statement and print all your conditions and you will see which condition is false in debug log.
Thanks Paul. I tried this, and got the following valdation error. I have no idea what it means. Any suggestions?
Thanks.
Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger LeadTrigger caused an unexpected exception, contact your administrator: LeadTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.LeadTrigger: line 21, column 1".
Line 21 is the following line of code:
System.debug('1 condition:' + LeadtaskMap.get(tsk.whoid).Description == null);
Are you testing with task related to lead? Also make sure that lead is not converted.
Did you add system.debug before if condition that you trying to test? like this:
It does not make sense that it throws null object error if you already checked that leadTaskMap contains tsk.whoId.
Yep - am testing against a Task related to a Lead, and the Lead isnt converted. I moved the debug code to just before the condition that im trying to test. However, now i dont get anything at all. The original issue is still there i.e. Lead status updating incorrectly. This is very bizarre.
I just noticed that you have typo ("Qualify Opportuntity") instead of "Qualify Opportunity". Try changing it to "Qualify Opportunity" and test again.
trigger LeadTrigger on Task (before insert, before update)
{
List<Id> leadIds=new List<Id>();
for(Task t:trigger.new)
{
if(t.Status=='Completed')
{
if(t.whoId != null && 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
map<id,Lead> LeadtaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE]);
for(Task tsk:trigger.new)
{
if(LeadtaskMap.keyset().contains(tsk.whoid))
{
if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead')
{
LeadtaskMap.get(tsk.whoid).Status = 'Contacted';
}
System.debug('1 condition:' + LeadtaskMap.get(tsk.whoid).Description == NULL);
System.debug('2 condition:' + tsk.Status == 'Completed');
System.debug('3 condition:' + tsk.Subject == 'Make contact with Lead');
System.debug('task:' + tsk);
if(LeadtaskMap.get(tsk.whoid).Description == NULL && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportunity')
{
tsk.adderror('Task cannot be completed until the following fields have been populated: Description');
}
Else
{
LeadtaskMap.get(tsk.whoid).Status = 'Qualified';
}
}
}
List<Lead> leadList = LeadtaskMap.values();
update leadList;
}
The Lead Status is now updating correctly. However, I am not getting an error message when the Lead Description field is Null. Am i using the adderror function correctly?
Thanks
trigger LeadTrigger on Task (before insert, before update)
{
List<Id> leadIds=new List<Id>();
for(Task t:trigger.new)
{
if(t.Status=='Completed')
{
if(t.whoId != null && 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
map<id,Lead> LeadtaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE]);
for(Task tsk:trigger.new)
{
if(LeadtaskMap.keyset().contains(tsk.whoid))
{
if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead')
{
LeadtaskMap.get(tsk.whoid).Status = 'Contacted';
}
// System.debug('1 condition:' + LeadtaskMap.get(tsk.whoid).Description == NULL);
// System.debug('2 condition:' + tsk.Status == 'Completed');
// System.debug('3 condition:' + tsk.Subject == 'Make contact with Lead');
// System.debug('task:' + tsk);
Else if(LeadtaskMap.get(tsk.whoid).Description == Null && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Lead')
{
tsk.adderror('Task cannot be completed until the following fields have been populated: Description');
}
Else
{
LeadtaskMap.get(tsk.whoid).Status = 'Qualified';
}
}
}
List<Lead> leadList = LeadtaskMap.values();
update leadList;
}