Skip to main content
Wes McCarthy ha fatto una domanda in #Sales And Marketing
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';

 

            }

 

      }

 

        

 

    } 

 

    

 

    }
3 risposte
0/9000