1

I need some assistance.

Aim : create a Trigger that creates a task from a lead record when certain criteria is met.

Lead criteria :

Object : lead RT: Registration lead Source : Property

Task criteria ;

Status : not started Priority : normal Subject : should be populated with lead source picklist value Type: action Owner : lead owner

This is my logic ( see below) however every-time I create a lead with the criteria listed above I am not getting a task being generated

Really looking forward to your help

trigger CreateTaskOnLead on Lead (after insert) {
    List<Task> lTask = new List<Task>();
      Task t;
      if(Trigger.isAfter) {
        if(Trigger.isInsert) {
           for(Lead l: Trigger.new) {

               if((l.LeadSource != null)&&(l.LeadSource == 'Property')){
                 t = new Task(); 
                 t.OwnerId = l.OwnerId;
                 t.Subject = 'Property';
                 t.Priority = 'Normal';
                 t.Status = 'Not Started';
                 t.Type = 'Action';
                 lTask.add(t);   
               }
            }
            if(!lTask.IsEmpty())
                insert t;
           }        
      }
}
  • 1
    Try adding a WhoId that points to your Lead ID ... – user735 Jan 19 '15 at 21:47
  • John Westenhaver, as in WhoId = trigger.new[i].Id; – Masond3 Jan 19 '15 at 21:49
  • Try this t.WhoId = l.Id; – Mihai Neagoe Jan 19 '15 at 22:26
2

I do not see where you are checking for record type is your trigger so going to add that in the answer. As per others commented you need to set the whatID for the Owner.

Another issue you need to account for is assignment rules. If the owner changes then you may want to reassign the tasks to the new lead owner. You would query for the tasks related to the leads where the owner is changing and update the task owner appropriatly

trigger CreateTaskOnLead on Lead (after insert) {
    Map<ID,schema.RecordTypeInfo> lead_RT = lead.sObjectType.getDescribe().getRecordTypeInfosByID();

    List<Task> lTask = new List<Task>();

      if(Trigger.isAfter && trigger.isInsert) {
           for(Lead l: Trigger.new) {

               if(l.LeadSource == 'Property'){
                 if(lead_rt.get(l.RecordTypeID).getName() != 'Registration') continue;
                 task t = new Task( 
                 whatID = l.id,
                 Subject = 'Property',
                 Priority = 'Normal',
                 Status = 'Not Started',
                 Type = 'Action',
                 ownerID - l.ownerID
                 );
                 lTask.add(t);   
               }
            }
        insert t;
      }
}
  • 1
    Nice. Two things, though: (1) you could use a describe instead of a query to get the record type (getRecordTypeInfosByName), and (2) you no longer need to see if a list is empty before calling a DML operation-- it automatically becomes a zero-cost no-op if the list is empty. – sfdcfox Jan 19 '15 at 23:03
  • @sfdcfox - Good points - updated answer. I remember you saying about the zero-cost before just did not change the OP code to that....Always good to update practices.... – Eric Jan 19 '15 at 23:13
0

@Eric & @sfdcfox

Thanks for all your responses its much appreciated. You mentioned that i havent referenced a record type within my trigger.

when i copy your code i get the following error message

I am getting the following error Error: Compile Error: expecting a left parentheses, found 'lead_RT.isEmpty' at line 5 column 7

I am a novice when it comes to apex and triggers. So i do appreciate all your help and advice.

Is it best practice that i reference the Registration Record ID within the trigger its self ?

Registration record type id is 012R000000051OZ

  • 1
    this should be a comment on my answer. See my updated code. and NO it is NOT best practice to hard code any id in your code – Eric Jan 20 '15 at 13:59
  • Hi Eric , Thanks for your swift reponse. After your updated code i get the following error Error: Compile Error: Invalid field initializer: t.whatID at line 12 column 18 – Masond3 Jan 20 '15 at 14:25
  • Sorry, I forgot to modify your code. Remove all of the t. - See updated code – Eric Jan 21 '15 at 2:54
-1
trigger CreateTaskOnLead on Lead (after insert) {
    List<Task> lstTask = new List<Task>();
      Task objTask;
      if(Trigger.isAfter) {
        if(Trigger.isInsert) {
           for(Lead objLead: Trigger.new) {
               if((l.LeadSource != null)&&(l.LeadSource == 'Property')){
                 objTask = new Task(); 
                 objTask.OwnerId = objLead.OwnerId;
                 objTask.Subject = 'Call';
                 objTask.Priority = 'Normal';
                 objTask.Status = 'Not Started';
                 objTask.Description = objLead.Description;
                 objTask.WhoID = objLead.id;
                 lstTask.add(objTask);   
               }
            }
            if(!lstTask.IsEmpty())
                insert objTask;
           }        
      }
}
  • Could you please try to add description for this? – Pranay Jaiswal Jun 8 '16 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.