• nguyen tai
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
I'm not sure why it's failing when I try to run 1000 records through. Anything I can do? 

Here's my Trigger:
trigger PlaybooksEnrollmentLead on Lead (before update, after update) {
    
    if(trigger.isbefore) {
        Map<Id, Lead> leadMap = new Map<Id, Lead>();
        for(Lead l : trigger.new) {
            Lead oldLead = trigger.oldMap.get(l.Id);
    
            if(oldLead.Playbooks_Play_Name__c != l.Playbooks_Play_Name__c && l.Playbooks_Play_Name__c != null) {
                // Pass this to class that creates new Enrollment
                PlaybooksEnrollmentEngine.CreateRecordLead(trigger.new);
            } else if(oldLead.Playbooks_Play_Status__c != l.Playbooks_Play_Status__c  ||
                       oldLead.Playbooks_Step_Number__c != l.Playbooks_Step_Number__c) {
                leadMap.put(l.Id, l);
                PlaybooksEnrollmentEngine.UpdateEnrollmentLead(leadMap);
            }
        }
    }

    if(trigger.isAfter) {
        Map<Id, Lead> leadMap = new Map<Id, Lead>();
        for(Lead l : trigger.new){
            if(l.IsConverted == TRUE && l.Playbooks_Play_Name__c != null && l.Playbooks_Play_Status__c != 'Failure' && l.Playbooks_Play_Status__c != 'Unenrolled') {
                leadMap.put(l.Id, l);
                PlaybooksEnrollmentEngine.LeadConverted(leadMap);
            }
        }
    }
    
}
And here's my Class:
public class PlaybooksEnrollmentEngine {

    public static void CreateRecordLead(List<Lead> leads) {
        
        // Create a list that inserts the PB Enrollment records
        List<Playbooks_Enrollment__c> insertList = new List<Playbooks_Enrollment__c>();

        for(Lead l : leads){
            // Create new Playbooks Enrollment record
            Playbooks_Enrollment__c pbE = new Playbooks_Enrollment__c();

            pbE.Object_Enrolled__c = 'Lead';
            pbE.Lead__c = l.Id;
            pbE.Name             = l.Playbooks_Play_Name__c;
            pbE.Enroller__c      = l.LastModifiedById;
            pbE.Play_Name__c     = l.Playbooks_Play_Name__c;
            pbE.Next_Step_Due__c = l.Playbooks_Next_Step_Due_Date__c;
            pbE.Play_Status__c   = l.Playbooks_Play_Status__c;
            pbE.Step_Number__c   = l.Playbooks_Step_Number__c; 

            if(!insertList.contains(pbE)){
                insertList.add(pbE);
            }
        }

        insert insertList;
    }
}

It's currently happening when I update the fields to trigger on Before Update, not the conversion part. Any help is appreciated, thanks!