• Steven Wellman 11
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    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!
Here's my Trigger:
trigger SoldCustomer on Opportunity (after update) {
    for (Opportunity opp : Trigger.new) {
        if (opp.Account.Type != 'Customer' && opp.StageName == 'Sold' && opp.Account.Type != null) {
            opp.Account.Type = 'Customer';
        }
    }
}

Here's my Test Class:
@isTest
private class SoldCustomerTest {

    @isTest static void CreateOpp() {
        // Create Account
        Account acc = new Account();

        acc.Name = 'Created Account';
        acc.Type = 'Prospect';
        String accId = acc.Id;

        Opportunity opp = new Opportunity();
        opp.StageName = 'Discovery';
        opp.CloseDate = date.parse('12/27/2019');
        opp.Name = 'Sold Customer Test Opp';
        opp.AccountId = accId;
    }

    @isTest static void UpdateOpp() {
        List<Opportunity> records = [
        SELECT Id FROM Opportunity WHERE Name = 'Sold Customer Test Opp'
        ];

        for (Opportunity record : records) {
            record.StageName = 'Sold';
        }

        update records;
    }
}
When I run the test class, it shows no coverage for the trigger. This is my 2nd trigger and I'm really excited about asking my first question. Hopefully, someone can help. Thanks in advance!