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

Problems deploying Trigger that test out perfectly in Sandbox

Hi Everyone, hoping there is someone that is seeing something I'm not.  I have the following trigger, setRegPartnerTrigger that calls the APEX Class setRegPartner. The test code is testSetRegPartner. The test code executes with no issues in sandbox, the test leads are inserted, updated and result in 95% plus coverage. When I try to deploy, the test code errors out telling me it can't insert the first lead. 

I have run the code from the trigger and the test lead insert in an execute anonomyous window successfully so am completely stumped.  Hoping someone sees something I'm missing. 

public class setRegPartner{

    public static void setPartnerLookUp(Set<Id> dealRegIds){
        system.debug('number of leads received by SetPartnerOnDealReg class>>>'+dealRegIds.size());
        List<Lead> dealRegs = new List<Lead>();
        //put the leads in the trigger into a list for processing
        dealRegs = [SELECT id, Name, FirstName, LastName, Email, Phone, City, State, PostalCode, Company,
                            Deal_Registration_Status__c, Deal_Product__c, Deal_Registration_Date__c, deal_exp_close_date__c,
                            Deal_Registration_Expiration__c, Registered_Reseller__c, Preferred_Reseller__c,
                            Reseller_Sales_Person__c, Reseller_Phone_Number__c, Registered_Partner_Rep_Email__c,
                            Reseller_Region__c, Distributor__c, Deal_Distributor__c, Reseller_Rep__c
                            FROM Lead WHERE id IN :dealRegIds];
        system.debug('Deal Reg '+dealRegs);
        system.debug('number of leads in dealRegs List>>>'+dealRegs.size());
        //put partner account and reps into maps for later retrieval
        Map<String, Account> ptnrAccts = new Map<String, Account>();
        Map<String, Contact> ptnrReps = new Map<String, Contact>();
        Map<string, Account> ptnrDomains = new map<String,Account>();
        Set<String> acctTypes = new Set<String>();
        acctTypes.add('Var- Other');
        acctTypes.add('Var- Preferred');
        List<Account> ptnrs = new List<Account>();
        ptnrs = [SELECT id, Name, Type, OwnerId, email_domain__c FROM Account WHERE
                 Type IN :acctTypes];

        system.debug('partner type VAR- Other'+ptnrs);
        set<id> ptnrAcctIds = new Set<id>();
        set<string> emailset = new set<string>();
            for(Account a : ptnrs){
                ptnrAccts.put(a.Id, a);
                    ptnrDomains.put(a.email_domain__c, a);
            List<Contact> repList = [SELECT id, AccountId, FirstName, LastName, Email,
                          Phone, Account.OwnerId FROM Contact
                          WHERE AccountId IN :ptnrAcctIds];
            for(Contact c : repList){
                    system.debug('email for rep>>>';
                    ptnrReps.put(, c);
            system.debug('contact emails----   '+emailset);
            List<Contact> contactsToInsert = new List<Contact>();
        for(Lead l : dealRegs)
            if (emailset.contains(l.Registered_Partner_Rep_Email__c))
              system.debug('email exist');
              system.debug('doesnt exist');
        //system.debug('print partner Account'+ptnrAccts);
        //system.debug('print partner Contact'+ptnrReps);
       Map<String, Account> distyAccts = new Map<String, Account>();
    //put distributors accounts into map
       List<Account> distys = new List<Account>();
        distys = [SELECT id, Name, Type, OwnerId FROM Account WHERE Type = 'Distributor'];
            for(Account d : distys){
                distyAccts.put(d.Name, d);

        list<Lead> leadsToUpdate = new List<Lead>();
        for(Lead l : dealRegs) {
          String email =  l.Registered_Partner_Rep_Email__c;
                list <String> extractEmail = email.split('@');
                system.debug('email array'+ extractEmail);
                email = extractEmail[1];
            if(emailset.contains(l.Registered_Partner_Rep_Email__c)) {
                    string acctId = ptnrReps.get(l.Registered_Partner_Rep_Email__c).Accountid;
                    l.Reseller_Rep__c = ptnrReps.get(l.Registered_Partner_Rep_Email__c).id;
                    l.Registered_Reseller__c = acctId;
                    l.Deal_Distributor__c = distyAccts.get(l.Distributor__c).id;
                    l.OwnerId = ptnrReps.get(l.Registered_Partner_Rep_Email__c).Account.OwnerId;
            } else if(!emailset.contains(l.Registered_Partner_Rep_Email__c) && ptnrDomains.get(email) != NULL){
                system.debug('doesnt exist');
                        String fName = l.Reseller_Sales_Person__c.left(l.Reseller_Sales_Person__c.indexOf(' '));
                        String lName = l.Reseller_Sales_Person__c.right(l.Reseller_Sales_Person__c.length()-

            l.Reseller_Sales_Person__c.indexOf(' '));                                              
                        Contact newContact = new Contact();
                        newContact.FirstName = fName;
                        newContact.LastName = lName;
                        newContact.Email =l.Registered_Partner_Rep_Email__c;
                        newContact.Phone = l.Reseller_Phone_Number__c;
                        newContact.AccountId = ptnrDomains.get(email).id;
                        system.debug('insert contact ' +newContact);
                        insert newContact;
                      l.Reseller_Rep__c =;
                      l.Registered_Reseller__c = newContact.AccountId;
                      l.Deal_Distributor__c = distyAccts.get(l.Distributor__c).id;
            } else {
                   l.Partner_Status__c = 'Not Found';
        update leadsToUpdate;

trigger setRegPartnerTrigger on Lead (after insert) {
  Set<id> leadIds = new Set<id>();

    for(Lead l :{
        if(l.LeadSource == 'Deal Registration'){
    system.debug('number of leads sent to setRegPartner class>>>'+leadIds.size());   

@isTest (seeAllData=true)
public class testSetRegPartner{
    static testmethod void testRegPartner(){
       Account disty = new Account(Name='Ingram Micro', Type='Distributor', Customer_ID__c = 'ING4051', email_domain__c = '');
       insert disty;
       Account ptnr = new Account(Name='Partner', Type='VAR- Other', Customer_ID__c = 'other123', email_domain__c = '');
       insert ptnr;
       Contact rep = new Contact(FirstName='Pard', LastName='Rep', Email = '', Accountid =;
         insert rep;
       Contact distyRep = new Contact(FirstName='disty', lastName='Rep', Email='',, Deal_Registration_Contact__c = 'yes');
        insert distyRep;
       String DealRegRecTypeId = [SELECT id FROM RecordType WHERE SobjectType = 'Lead' and Name ='Partner Deal Registrations'].id;
      Lead noRepAcct = new Lead();
          noRepAcct.RecordTypeId = DealRegRecTypeId;
            noRepAcct.LeadSource = 'Deal Registration';
            noRepAcct.FirstName = 'first';
            noRepAcct.LastName = 'last';
            noRepAcct.Email = '';
            noRepAcct.City = 'mapleton';
            noRepAcct.State = 'IL';
            noRepAcct.PostalCode = '12345';
            noRepAcct.Company = 'test';
            noRepAcct.Deal_Registration_Status__c = 'Submitted';
            noRepAcct.Deal_Product__c = 'B800i';
            noRepAcct.Deal_Registration_Date__c =;
            noRepAcct.deal_exp_close_date__c =;
            noRepAcct.Preferred_Reseller__c = 'testReseller';
            noRepAcct.Reseller_Sales_Person__c = 'Test Rep';
            noRepAcct.Reseller_Phone_Number__c = '123-123-1234';
            noRepAcct.Registered_Partner_Rep_Email__c = '';
            noRepAcct.Distributor__c = 'Ingram Micro';
            noRepAcct.deal_unit_qty__c = 5;
        insert noRepAcct;
        Lead acctNoRep = new Lead();
            acctNoRep.RecordTypeId = DealRegRecTypeId;
            acctNoRep.LeadSource = 'Deal Registration';
            acctNoRep.FirstName = 'first';
            acctNoRep.LastName = 'last';
            acctNoRep.Email = '';
            acctNoRep.City = 'mapleton';
            acctNoRep.State = 'IL';
            acctNoRep.PostalCode = '12345';
            acctNoRep.Company = 'test';
            acctNoRep.Deal_Registration_Status__c = 'Submitted';
            acctNoRep.Deal_Product__c = 'B800i 8TB';
            acctNoRep.Deal_Registration_Date__c =;
            acctNoRep.deal_exp_close_date__c =;
            acctNoRep.Preferred_Reseller__c = 'testReseller';
            acctNoRep.Reseller_Sales_Person__c = 'Test Rep';
            acctNoRep.Reseller_Phone_Number__c = '123-123-1234';
            acctNoRep.Registered_Partner_Rep_Email__c = '';
            acctNoRep.Distributor__c = 'Ingram Micro';
            acctNoRep.deal_unit_qty__c = 5;
        insert acctNoRep;
        Lead acctRep = new Lead();
          acctRep.RecordTypeId = DealRegRecTypeId;
            acctRep.LeadSource = 'Deal Registration';
            acctRep.FirstName = 'first';
            acctRep.LastName = 'last';
            acctRep.Email = '';
            acctRep.City = 'mapleton';
            acctRep.State = 'IL';
            acctRep.PostalCode = '12345';
            acctRep.Company = 'test';
            acctRep.Deal_Registration_Status__c = 'Submitted';
            acctRep.Deal_Product__c = 'B800i';
            acctRep.Deal_Registration_Date__c =;
            acctRep.deal_exp_close_date__c =;
            acctRep.Preferred_Reseller__c = 'testReseller';
            acctRep.Reseller_Sales_Person__c = 'Test Rep';
            acctRep.Reseller_Phone_Number__c = '123-123-1234';
            acctRep.Registered_Partner_Rep_Email__c = '';
            acctRep.Distributor__c = 'Ingram Micro';
            acctRep.deal_unit_qty__c = 5;
        insert acctRep;
        system.assertEquals(acctNoRep.Partner_Status__c, NULL);
        system.assertEquals(acctRep.Partner_Status__c, NULL);
        system.assertEquals(noRepAcct.Partner_Status__c, NULL);
        Set<id> dealRegIds = new Set<id>();
        acctRep.Deal_Registration_Status__c = 'Approved';
        update acctRep;

    List<Lead> testLeads = new List<Lead>();
    convertLead c = new convertLead();
    List<Lead> convLead = [SELECT id, Distributor__c, ConvertedOpportunityId FROM
                Lead WHERE id =];
    Quote q = [SELECT id, Distributor__c FROM Quote WHERE OpportunityId = :convLead[0].ConvertedOpportunityId];
    acctNoRep.Deal_Registration_Status__c = 'Approved';
        update acctNoRep;
    List<Lead> testLeads2 = new List<Lead>();
    convertLead d = new convertLead();
    List<Lead> convLead2 = [SELECT id, Distributor__c, ConvertedOpportunityId FROM
                Lead WHERE id =];
    Quote q2 = [SELECT id, Distributor__c FROM Quote WHERE OpportunityId = :convLead2[0].ConvertedOpportunityId];
    List<QuoteLineItem> qlis = [SELECT id FROM QuoteLineItem WHERE QuoteID =];
        system.debug('test code qlis size is>>>'+qlis.size());
    system.assertEquals(qlis.size(), 1);

Adnubis LLCAdnubis LLC
Are you able to provide a little more detail about the error? If you could post the Error in its entirety I may be able to find the issue. 
Error we are getting in testSetRegPartner.testRegPartner() on line no. 36

Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, setRegPartnerTrigger: execution of AfterInsert caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows).
It turned out this was due to the number of records in the production environment. I cleaned up how the SOQL queries using the pattern found in the documentation ( on working with very large queries. Additionally, I removed the (viewAllData=True)  flag and it worked perfectly.