+ Start a Discussion
iKnowSFDCiKnowSFDC 

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. 

APEX CLASS: 
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');
        acctTypes.add('DMR');
        acctTypes.add('Etail');
        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);
                ptnrAcctIds.add(a.id);
                if(string.isNotblank(a.email_domain__c)){
                    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>>>'+c.email);
                    ptnrReps.put(c.email, c);
                    emailset.add(c.email);
                }
           
            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');
              else
              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;
                    leadsToUpdate.add(l);                        
               
            } 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 = newContact.id;
                      l.Registered_Reseller__c = newContact.AccountId;
                      l.Deal_Distributor__c = distyAccts.get(l.Distributor__c).id;
                      leadsToUpdate.add(l);
               
            } else {
                   l.Partner_Status__c = 'Not Found';
                    leadsToUpdate.add(l);                        
                }
            }
        update leadsToUpdate;
    }
}

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

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

TEST CODE: 
@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 = 'ingram.com');
       insert disty;
       Account ptnr = new Account(Name='Partner', Type='VAR- Other', Customer_ID__c = 'other123', email_domain__c = 'partner.com');
       insert ptnr;
       Contact rep = new Contact(FirstName='Pard', LastName='Rep', Email = 'pard@partner.com', Accountid = ptnr.id);
         insert rep;
       Contact distyRep = new Contact(FirstName='disty', lastName='Rep', Email='disty@rep.com', Accountid=disty.id, 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 = 'a@b.com';
            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 = date.today();
            noRepAcct.deal_exp_close_date__c = date.today().addDays(30);
            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 = 'tr@tp.com';
            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 = 'a@b.com';
            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 = date.today();
            acctNoRep.deal_exp_close_date__c = date.today().addDays(30);
            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 = 'tr@partner.com';
            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 = '1@b.com';
            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 = date.today();
            acctRep.deal_exp_close_date__c = date.today().addDays(30);
            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 = 'pard@partner.com';
            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>();
        dealRegIds.add(acctRep.id);
       
        setRegPartner.setPartnerLookUp(dealRegIds);
        acctRep.Deal_Registration_Status__c = 'Approved';
        update acctRep;

       
    List<Lead> testLeads = new List<Lead>();
    testLeads.add(acctRep);
   
    convertLead c = new convertLead();
    c.convertLeads(testLeads);
    List<Lead> convLead = [SELECT id, Distributor__c, ConvertedOpportunityId FROM
                Lead WHERE id = :acctRep.id];
               
    Quote q = [SELECT id, Distributor__c FROM Quote WHERE OpportunityId = :convLead[0].ConvertedOpportunityId];
   
    c.sendDistyEmail(q);
   
    acctNoRep.Deal_Registration_Status__c = 'Approved';
        update acctNoRep;
       
    List<Lead> testLeads2 = new List<Lead>();
    testLeads2.add(acctNoRep);
   
    convertLead d = new convertLead();
    d.convertLeads(testLeads2);
    List<Lead> convLead2 = [SELECT id, Distributor__c, ConvertedOpportunityId FROM
                Lead WHERE id = :acctNoRep.id];
               
    Quote q2 = [SELECT id, Distributor__c FROM Quote WHERE OpportunityId = :convLead2[0].ConvertedOpportunityId];
    List<QuoteLineItem> qlis = [SELECT id FROM QuoteLineItem WHERE QuoteID = :q2.id];
        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. 
javajo13javajo13
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).
iKnowSFDCiKnowSFDC
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 (https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_VLSQ.htm) on working with very large queries. Additionally, I removed the (viewAllData=True)  flag and it worked perfectly.