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
Priscilla PresleyPriscilla Presley 

after trigger causes recursion with .size() list method

Hi,

I am trying to make a trigger where that returns the number of key fields that have been populated into a custom number field, before creation of the record. Upon creation of the record, for each key field that has been populated, I want to create a task record and associate it to the Lead.

I am having trouble with a statement causing recursion when including the 'after insert' event, which I believe I require to return the Lead record Id which will be needed for the What.Id of the task.

I have posted the code below, highlighting in bold, the line that causes the error.

Thank you all!

trigger KeyFields on Lead (before insert) {
    List<integer> keyFields = new List<integer>();
    for (Lead newLead : Trigger.new) {
        if(newLead.FirstName == null) {
            keyFields.isEmpty();
            } else { keyFields.add(1); }
        if (newLead.LastName == null) {
                keyFields.isEmpty();
            } else {
                keyFields.add(2);
            }
        if (newLead.Email == null) {
                    keyFields.isEmpty();
        } else { keyFields.add(3);
            
        }    
        if (newLead.Phone == null) {
                        keyFields.isEmpty();
        } else { keyFields.add(4);
        }
        if (newLead.Website == null) {
                            keyFields.isEmpty();
        } else { keyFields.add(5);
            
        }
        if (newLead.Title == null){
                                keyFields.isEmpty();
        } else { keyFields.add(6);} 
        keyFields.size();
        Decimal sizeOfList = keyFields.size();
        newLead.Key_Fields_Populated__c = sizeOfList;
        
        // from the Set, return the elements that have been populated into the list
        // if the size of the set is greater than 3.
        List<integer> fields = new List<integer>();
        if(sizeOfList >=3) {
            for (Integer i=0; i < sizeOfList; i++){
                Integer num = i;
                Integer indexNumber = keyFields.get(i);
                if (!keyFields.isEmpty()){ 
                    fields.add(indexNumber);
               }
      }
}
        //System debug - look to see if the fields list is being populated.
        for (Integer checkfields : fields) {
            System.debug(checkfields);
        }
        List<String> fieldNames = new List<String>();
        fieldNames.add('First Name');
        fieldNames.add('Last Name');
        fieldNames.add('Email');
        fieldNames.add('Phone');
        fieldNames.add('Website');
        fieldNames.add('Title');
        
        for (Integer i=0; i <fields.size(); i++){
            Integer loopNumber = fields.get(i);
            Task t = new Task();
            t.WhatId = newLead.id;
            t.Subject = 'Verify the' + fieldNames.get(loopNumber) + 'fields';
            t.Status = 'Not Started';
            t.WhoId = newLead.OwnerId;
            t.ActivityDate = date.today();
        }
      }
}
sachinarorasfsachinarorasf
Hi Priscilla Presley,

You should look into the following link for avoiding recursion on the trigger.

https://developer.salesforce.com/forums/?id=906F0000000Qtw4IAC

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com