You need to sign in to do that
Don't have an account?
Wes McCarthy
Issue with Lead Task Trigger
Hi there,
I am trying to run a Task trigger on a Lead as follows.
If Task Subject is A, then update the Lead Status upon Closure of Task.
If Task Subject is B, then check value of Description field on Lead.
If null, return an error.
If not null, then update the Status of the Lead upon closure of the Task.
Code is below, however it doesnt seem to be working. Would appreciate any advice.
Many 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.whatid))
{
if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead')
{
LeadtaskMap.get(tsk.whatid).Status = 'Contacted';
}
if(LeadtaskMap.get(tsk.whatid).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.whatid).Status = 'Qualified';
}
}
}
I am trying to run a Task trigger on a Lead as follows.
If Task Subject is A, then update the Lead Status upon Closure of Task.
If Task Subject is B, then check value of Description field on Lead.
If null, return an error.
If not null, then update the Status of the Lead upon closure of the Task.
Code is below, however it doesnt seem to be working. Would appreciate any advice.
Many 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.whatid))
{
if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead')
{
LeadtaskMap.get(tsk.whatid).Status = 'Contacted';
}
if(LeadtaskMap.get(tsk.whatid).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.whatid).Status = 'Qualified';
}
}
}
update leadList;
and then after it saved I uncommented this line and was able to save trigger.
All Answers
if(LeadtaskMap.keyset().contains(tsk.whatid))
I think you should use tsk.whoid - As you queried the leads based on whoIds.
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';
}
}
}
}
I don't see where you update leads in this code.
Try adding this code as a last line of code in trigger: 1 List<Lead> leads = LeadtaskMap.values();
Error: Compile Error: line 40:4 no viable alternative at character '' at line 40 column 4
{
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;
}
update leadList;
and then after it saved I uncommented this line and was able to save trigger.
the adderror function does not appear to be working.