Skip to main content
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;

    }
9 个回答
  1. 2015年12月16日 03:07
    Ok.  I now have this working.  Code below.  Thanks for all of your help.  Much appreciated.

    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;

          }
0/9000