function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rodolfo RitoRodolfo Rito 

Help in Trigger with Tasks

trigger TasksonLeads on Lead (after update) {

    for (Lead l : Trigger.new) {  
        
        if (l.status == 'Awaiting First Call Attempt' 
        && l.aux_reporting_min_req_mql_tasks__c == false
        && l.aux_reporting_first_assigned__c == true
        && l.aux_scoring_first_attempt__c == false
        && l.min_req_mql_landlord__c == null)
    
            {
        
                Task Landlord = new Task();
            
                    String userId = UserInfo.getUserId();
        
                    Landlord.Subject = '1. Is it a landlord?';
                    Landlord.Priority = 'Normal';
                    Landlord.Status = 'Open';
                    Landlord.OwnerId = userId;
                    Landlord.WhatId = l.Id;
                   
                insert Landlord;
                
             }
        
        }
        
}

How can I put the Task in the correct Lead ?

It's giving me this error :

User-added image

Thanks!
Manohar kumarManohar kumar

Hi,

if you are using after update then i think you have to put trigger.old. Use trigger.new if you are using after insert.

please let me know if this helps.

Thanks,

Manhar 

 

Rodolfo RitoRodolfo Rito
It helped, but it's not creating me the task and I don't understand why.

Already tried with after update (and trigger.old) and after insert (and trigger.new).

 
Manohar kumarManohar kumar

Hi Rodolfo,

Please try WhoId instead WhatId.

Landlord.WhoId = l.Id; 

Thanks,

Manohar

Rodolfo RitoRodolfo Rito
it's still not working. grrr....

It should work when l.aux_reporting_first_assigned__c turns true.

which event should I use then?
Sagar LakhaniSagar Lakhani

Hi,

Manohar is correct you simply have to replace whoId in place of whatId then its working fine and you also have to ensure that all if conditions get satisfied,

Thanks,

Sagar Lakhani

Rodolfo RitoRodolfo Rito
trigger CreateMinReqTasks on Lead (after update) {

    for (Lead l : Trigger.old) {  
    
     if(l.status == 'Awaiting First Call Attempt' 
        && l.aux_reporting_min_req_mql_tasks__c == false
        && l.aux_reporting_first_assigned_date__c != null
        && l.aux_scoring_first_attempt__c == false
        && l.min_req_mql_landlord__c == null)
        
    
            {
        
                Task t = new Task();
            
                   String userId = UserInfo.getUserId();
        
                    t.Subject = '1. Is it a landlord?';
                    t.Priority = 'Normal';
                    t.Status = 'Open';
                    t.OwnerId = userId;
                    t.WhoId = l.Id;
                   
                insert t;
                
             }
        
        }
        
}

It is not creating the task!
Manohar kumarManohar kumar

Hi Rodolfo,

i think you should go with before update.

Thanks,

Manohar

Sagar LakhaniSagar Lakhani

Hi Rodolfo,

 

Plaese change Trigger.old to Trigger.New then do test its definitely works.

 

Thanks,

Sagar lakhani

 

 

Rodolfo RitoRodolfo Rito
User-added image
Sagar LakhaniSagar Lakhani

Hi Rodolfo,

you don't need to change after to before you only have to replace trigger.old to trigger.new.

Thanks,

Sagar lakhani

Rodolfo RitoRodolfo Rito
trigger CreateMinReqTasks on Lead (before update) {

    for (Lead l : Trigger.new) {  
    
     if(l.status == 'Awaiting First Call Attempt' 
        )
        //&& l.aux_reporting_min_req_mql_tasks__c == false
        //&& l.aux_reporting_first_assigned_date__c != null
       // && l.aux_scoring_first_attempt__c == false
       // && l.min_req_mql_landlord__c == null
    
            {
        
                Task t = new Task();
            
                   String userId = UserInfo.getUserId();
        
                    t.Subject = '1. Is it a landlord?';
                    t.Priority = 'Normal';
                    t.Status = 'Open';
                    t.OwnerId = userId;
                    t.WhoId = l.Id;
                   
                insert t;
                
             }
        
        }
        
}
User-added image
 
Sagar LakhaniSagar Lakhani

Hi Rodolfo,

 

This looks like a Recursive Trigger issue. Usually when you have maximum trigger depth exception this means that a trigger is getting recursively fired. You can only have something recursively fire 16 times (Max Stack Depth).

Please go through below link for solution

http://www.infallibletechie.com/2014/05/maximum-trigger-depth-exceeded.html

 

Thanks,

Sagar Lakhani

Sagar LakhaniSagar Lakhani
You can also go through below video and Go to minute 14 where it shows Maximum Depth Trigger and how to prevent recursion. https://www.youtube.com/watch?v=rCDSZ8_UpVs#t=848
Manohar kumarManohar kumar

Same trigger works for me.

You have to handle recursion.Make a class like below

public class P { 
   public static boolean firstRun = true; 
}
 
trigger CreateMinReqTasks on Lead (before update) {

    for (Lead l : Trigger.new) {  
    if(p.firstRun) {
     if(l.status == 'Awaiting First Call Attempt' 
        )
        //&& l.aux_reporting_min_req_mql_tasks__c == false
        //&& l.aux_reporting_first_assigned_date__c != null
       // && l.aux_scoring_first_attempt__c == false
       // && l.min_req_mql_landlord__c == null
    
            {
        
                Task t = new Task();
            
                   String userId = UserInfo.getUserId();
        
                    t.Subject = '1. Is it a landlord?';
                    t.Priority = 'Normal';
                    t.Status = 'Open';
                    t.OwnerId = userId;
                    t.WhoId = l.Id;
                   
                insert t;
                
             }
         }
        
        }
        
}
 

Please let us know if it works,

Thanks,

Manohar

Rodolfo RitoRodolfo Rito
it didn't work.

it didn't give an error but nothing happenned.

 
Rodolfo RitoRodolfo Rito
trigger CreateMinReqTasks on Lead (before update) {

    for (Lead l : Trigger.new) {  
    
        if(p.firstRun) {
    
        if(l.status == 'Awaiting First Call Attempt' 
            && l.aux_reporting_min_req_mql_tasks__c == false
            && l.aux_reporting_first_assigned_date__c != null
            && l.aux_scoring_first_attempt__c == false
            && l.min_req_mql_landlord__c == null
           )
        
    
             {
        
                Task t = new Task();
            
                   String userId = UserInfo.getUserId();
        
                    t.Subject = '1. Is it a landlord?';
                    t.Priority = 'Normal';
                    t.Status = 'Open';
                    t.OwnerId = userId;
                    t.WhoId = l.Id;
                   
                insert t;
                
             }
        
        }
        }
        
}
 
public class P {

   public static boolean firstRun = true;

}

User-added image
Alain CabonAlain Cabon
Hello,

You just used  Landlord.WhatId = l.Id; instead of   Landlord.WhoId = l.Id;  and code not bulkified using a list.

Try the code below and add your condition:
trigger TasksonLeads on Lead (after update) {
    List<Task> lt = new List<Task>();     
    for (Lead l : Trigger.new) {         
        Task Landlord = new Task();      
        String userId = UserInfo.getUserId();       
        Landlord.Subject = '1. Is it a landlord?';
        Landlord.Priority = 'Normal';
        Landlord.Status = 'Open';
        Landlord.OwnerId = userId;
        Landlord.WhoId = l.Id;       
        lt.add(Landlord);              
    }  
    if (!lt.isEmpty()) {
        insert lt;    
    }     
}

Alain
Manohar kumarManohar kumar

Hi Rodolfo,

Same thing works for me. put debug for your all conditions. I think any one of them is not true.

also put system.debug('p.id:'+p.id); to see if its getting created or not. Let me know if debugging not works.

Thanks,

Manohar

Ajay K DubediAjay K Dubedi
Hi Rodolfo Rito,
trigger CreateMinReqTasks on Lead (after update) {
    
    List<Task> taskList = new List<Task>();
    
    for (Lead l : Trigger.new) {      

     if(l.status == 'Awaiting First Call Attempt'

        && l.aux_reporting_min_req_mql_tasks__c == false
        && l.aux_reporting_first_assigned_date__c != null
        && l.aux_scoring_first_attempt__c == false
        && l.min_req_mql_landlord__c == null)

            {

                   Task t = new Task();

                    String userId = UserInfo.getUserId();
                    t.Subject = '1. Is it a landlord?';
                    t.Priority = 'Normal';
                    t.Status = 'Open';
                    t.OwnerId = userId;
                    t.WhoId = l.Id;
     
                 taskList.add(t);
             }
        }

  insert taskList;
}
 First you need to change Before update to After update and then change trigger.old to trigger.new and also you need to make you trigger Bulkfy.
 Regards,
 Ajay
Rodolfo RitoRodolfo Rito
I've tried with the class, and without the class, using after and before update.

It's not working.

Here's the last one:
 
trigger CreateMinReqTasks on Lead (after update) {

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

    for (Lead l : Trigger.new) {  
    
    if (p.firstRun) {
        
        if(l.status == 'Awaiting First Call Attempt' 
        
            && l.aux_reporting_min_req_mql_tasks__c == false
            && l.aux_reporting_first_assigned_date__c != null
            && l.aux_scoring_first_attempt__c == false
            && l.min_req_mql_landlord__c == null)
        
    
             {
        
                Task t = new Task();
            
                   String userId = UserInfo.getUserId();
                    t.Subject = '1. Is it a landlord?';
                    t.Priority = 'Normal';
                    t.Status = 'Open';
                    t.OwnerId = userId;
                    t.WhoId = l.Id;
                    
                    lt.add(t);
              }
         }
         
         }

             insert lt;
            
        }

User-added image