+ Start a Discussion
JN22JN22 

Error When Trying to Deploy to Production

Hello,

I am trying to deploy a trigger (UniqueDelivID) from my sandbox to production.  The trigger works fine in my sandbox and is covered 100% by the test class.  Yet when I try to move to production, I get the error below:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OppUpdates: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 00k7000000U8CVpAAN; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UniqueDelivID: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.UniqueDelivID: line 22, column 1: [] Trigger.OppUpdates: line 49, column 1: []
Stack Trace: Class.TestOppUpdates.TestOppUpdates: line 379, column 1


Does anyone know why I am getting this error?  Thanks.

Trigger (UniqueDelivID)
//Assigns a unique ID to each deliverable in every Opportunity so DyNad can track.
trigger UniqueDelivID on OpportunityLineItem (before insert, before update) {   

//if(checkRecursiveBI.runOnceBI()||checkRecursiveBU.runOnceBU())
//{
    Set<ID> setOliIds = new Set<ID>();
        for(OpportunityLineItem oli:Trigger.new){
            setOliIds.add(oli.Id);
        }
        Map<ID, OpportunityLineItem> mapOli = new Map<ID, OpportunityLineItem>([SELECT Id, Opportunity.Max_Deliv_Hx__c, Max_Deliv__c
                                                                                FROM OpportunityLineItem
                                                                                WHERE Id in:setOliIds]);
        if(mapOli.size()>0){

/*        List<OpportunityLineItem> oli1 = [SELECT Id, Max_Deliv__c
                                         FROM OpportunityLineItem
                                         WHERE Id IN: Trigger.newMap.keySet()]; 
                                     
            for (OpportunityLineItem oli :oli1){*/
            for(OpportunityLineItem oli1:Trigger.New){
                IF(mapOli.containsKey(oli1.Id) && (oli1.Max_Deliv__c == 0 || oli1.Max_Deliv__c == null)){
                    oli1.Max_Deliv__c = mapOli.get(oli1.Id).Opportunity.Max_Deliv_Hx__c + 1;
                }
            }
        }
//}

}

Test Class (TestOppUpdates)
// This class tests the trigger named OppUpdates.

@isTest(seeAllData=true)
public class TestOppUpdates {

    public static testMethod void TestOppUpdates() {       
                    
//Data Prep - Create Account, Opportunity, Product, etc.

        Account acct = TestCreateRecords.createAcct(0);
        insert acct;
        
    //Create Opportunity on Account
        Opportunity Opp = TestCreateRecords.createOpp(acct.Id);
       insert Opp;         
                       
    // Create Deliverables 

    List<product2> Insertprodlist= new list<product2>();

        //High-Risk Coaching
        Product2 deliv1 = new Product2 (name='Coaching-HR');
        deliv1.ProductCode = 'COACH_HR';
        deliv1.Product_Group__c = 'Coaching';
        Insertprodlist.add(deliv1);

        //Moderate-Risk Coaching
        Product2 deliv2 = new Product2 (name='Coaching-MR');
        deliv2.productcode = 'COACH_MR';
        deliv2.Product_Group__c = 'Coaching';
        Insertprodlist.add(deliv2);

        //Low-Risk Coaching
        Product2 deliv3 = new Product2 (name='Coaching-LR');
        deliv3.productcode = 'COACH_LR';
        deliv3.Product_Group__c = 'Coaching';
        Insertprodlist.add(deliv3);

        //Other Coaching
        Product2 deliv4 = new Product2 (name='Coaching-OTHER');
        deliv4.productcode = 'COACH_SELFREF';
        deliv4.Product_Group__c = 'Coaching';
        Insertprodlist.add(deliv4);

        //Weight Management
        Product2 deliv5 = new Product2 (name='Weight Management');
        deliv5.productcode = 'WEIGHTMGMTNODEVICE';
        deliv5.Product_Group__c = 'Weight Management';
        Insertprodlist.add(deliv5);

        //Tobacco Cessation
        Product2 deliv6 = new Product2 (name='Tobacco');
        deliv6.productcode = 'TOB_COACH';
        deliv6.Product_Group__c = 'Tobacco Cessation';
        Insertprodlist.add(deliv6);

        //Condition Management - Diabetes
        Product2 deliv7 = new Product2 (name='CM-Diabetes');
        deliv7.productcode = 'DIAB_COACH';
        deliv7.Product_Group__c = 'Condition Management';
        Insertprodlist.add(deliv7);

        //Condition Management - CHF
        Product2 deliv8 = new Product2 (name='CM-CHF');
        deliv8.productcode = 'HF_COACH';
        deliv8.Product_Group__c = 'Condition Management';
        Insertprodlist.add(deliv8);

        //Condition Management - CAD
        Product2 deliv9 = new Product2 (name='CM-CAD');
        deliv9.productcode = 'CAD_COACH';
        deliv9.Product_Group__c = 'Condition Management';
        Insertprodlist.add(deliv9);

        //Condition Management - COPD
        Product2 deliv10 = new Product2 (name='CM-COPD');
        deliv10.productcode = 'COPD_COACH';
        deliv10.Product_Group__c = 'Condition Management';
        Insertprodlist.add(deliv10);

        //Condition Management - Asthma
        Product2 deliv11 = new Product2 (name='CM-Asthma');
        deliv11.productcode = 'ASTH_COND_COACH';
        deliv11.Product_Group__c = 'Condition Management';
        Insertprodlist.add(deliv11);

        //Condition Management - Telephonic Bundle
        Product2 deliv12 = new Product2 (name='CM-Tel Bundle');
        deliv12.productcode = 'CMBUNDLE';
        deliv12.Product_Group__c = 'Condition Management';
        Insertprodlist.add(deliv12);

        //Health Concierge
        Product2 deliv13 = new Product2 (name='Health Concierge');
        deliv13.productcode = 'HCPLC_CGS';
        deliv13.Product_Group__c = 'Health Concierge';
        Insertprodlist.add(deliv13);
        
        //OWC
        Product2 deliv14 = new Product2 (name='Onsite Wellness Coordinators');
        deliv14.productcode = 'WELL_COORD';
        deliv14.Product_Group__c = 'Onsite Wellness Coordinators';
        Insertprodlist.add(deliv14);
        
        //Platform
        Product2 deliv15 = new Product2 (name='Core Package');
        deliv15.productcode = 'CORE_PKG';
        deliv15.Product_Group__c = 'Platform';
        Insertprodlist.add(deliv15);
        
        //Biometrics
        Product2 deliv16 = new Product2 (name='Biometrics');
        deliv16.productcode = 'BIOMETRIC_ONSITE';
        deliv16.Product_Group__c = 'Biometrics';
        Insertprodlist.add(deliv16);
        
        //Diabetes
        Product2 deliv17 = new Product2 (name='Diabetes');
        deliv17.productcode = 'DIAB_COACH';
        deliv17.Product_Group__c = 'Diabetes';
        Insertprodlist.add(deliv17);
        
        //Wellness Challenges
        Product2 deliv18 = new Product2 (name='Wellness Challenges');
        deliv18.productcode = 'WELLNESS';
        deliv18.Product_Group__c = 'Wellness Challenge';
        Insertprodlist.add(deliv18);
        
        //International - Platform
/*        Product2 deliv19 = new Product2 (name='International-Platform');
        deliv19.productcode = 'INT_PLAT';
        deliv19.Product_Group__c = 'International - Platform';
        Insertprodlist.add(deliv19);
        
        //International - Coaching
        Product2 deliv20 = new Product2 (name='International-Coaching');
        deliv20.productcode = 'INT_COACH';
        deliv20.Product_Group__c = 'International - Coaching';
        Insertprodlist.add(deliv20);*/
        
        insert Insertprodlist;
        
    // Get Pricebook
         Pricebook2 testpb = [select id from Pricebook2 where IsStandard = true];   
         
         List<PricebookEntry> InsertPricebookList= new List<PricebookEntry>();

// Add to pricebook
         PricebookEntry testdeliv1 = new PricebookEntry ();
         testdeliv1.pricebook2id = testpb.id;
         testdeliv1.product2id = deliv1.id;
         testdeliv1.IsActive = True;
         testdeliv1.UnitPrice = 10000;
         testdeliv1.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv1);
         
         PricebookEntry testdeliv2 = new PricebookEntry ();
         testdeliv2.pricebook2id = testpb.id;
         testdeliv2.product2id = deliv2.id;
         testdeliv2.IsActive = True;
         testdeliv2.UnitPrice = 10000;
         testdeliv2.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv2);
         
         PricebookEntry testdeliv3 = new PricebookEntry ();
         testdeliv3.pricebook2id = testpb.id;
         testdeliv3.product2id = deliv3.id;
         testdeliv3.IsActive = True;
         testdeliv3.UnitPrice = 10000;
         testdeliv3.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv3);
         
         PricebookEntry testdeliv4 = new PricebookEntry ();
         testdeliv4.pricebook2id = testpb.id;
         testdeliv4.product2id = deliv4.id;
         testdeliv4.IsActive = True;
         testdeliv4.UnitPrice = 10000;
         testdeliv4.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv4);
       
         PricebookEntry testdeliv5 = new PricebookEntry ();
         testdeliv5.pricebook2id = testpb.id;
         testdeliv5.product2id = deliv5.id;
         testdeliv5.IsActive = True;
         testdeliv5.UnitPrice = 10000;
         testdeliv5.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv5);
         
         PricebookEntry testdeliv6 = new PricebookEntry ();
         testdeliv6.pricebook2id = testpb.id;
         testdeliv6.product2id = deliv6.id;
         testdeliv6.IsActive = True;
         testdeliv6.UnitPrice = 10000;
         testdeliv6.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv6);
         
         PricebookEntry testdeliv7 = new PricebookEntry ();
         testdeliv7.pricebook2id = testpb.id;
         testdeliv7.product2id = deliv7.id;
         testdeliv7.IsActive = True;
         testdeliv7.UnitPrice = 10000;
         testdeliv7.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv7);
         
         PricebookEntry testdeliv8 = new PricebookEntry ();
         testdeliv8.pricebook2id = testpb.id;
         testdeliv8.product2id = deliv8.id;
         testdeliv8.IsActive = True;
         testdeliv8.UnitPrice = 10000;
         testdeliv8.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv8);
         
         PricebookEntry testdeliv9 = new PricebookEntry ();
         testdeliv9.pricebook2id = testpb.id;
         testdeliv9.product2id = deliv9.id;
         testdeliv9.IsActive = True;
         testdeliv9.UnitPrice = 10000;
         testdeliv9.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv9);
         
         PricebookEntry testdeliv10 = new PricebookEntry ();
         testdeliv10.pricebook2id = testpb.id;
         testdeliv10.product2id = deliv10.id;
         testdeliv10.IsActive = True;
         testdeliv10.UnitPrice = 10000;
         testdeliv10.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv10);
         
         PricebookEntry testdeliv11 = new PricebookEntry ();
         testdeliv11.pricebook2id = testpb.id;
         testdeliv11.product2id = deliv11.id;
         testdeliv11.IsActive = True;
         testdeliv11.UnitPrice = 10000;
         testdeliv11.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv11);
         
         PricebookEntry testdeliv12 = new PricebookEntry ();
         testdeliv12.pricebook2id = testpb.id;
         testdeliv12.product2id = deliv12.id;
         testdeliv12.IsActive = True;
         testdeliv12.UnitPrice = 10000;
         testdeliv12.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv12);
         
         PricebookEntry testdeliv13 = new PricebookEntry ();
         testdeliv13.pricebook2id = testpb.id;
         testdeliv13.product2id = deliv13.id;
         testdeliv13.IsActive = True;
         testdeliv13.UnitPrice = 10000;
         testdeliv13.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv13);
         
         PricebookEntry testdeliv14 = new PricebookEntry ();
         testdeliv14.pricebook2id = testpb.id;
         testdeliv14.product2id = deliv14.id;
         testdeliv14.IsActive = True;
         testdeliv14.UnitPrice = 10000;
         testdeliv14.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv14);
         
         PricebookEntry testdeliv15 = new PricebookEntry ();
         testdeliv15.pricebook2id = testpb.id;
         testdeliv15.product2id = deliv15.id;
         testdeliv15.IsActive = True;
         testdeliv15.UnitPrice = 10000;
         testdeliv15.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv15);
         
         PricebookEntry testdeliv16 = new PricebookEntry ();
         testdeliv16.pricebook2id = testpb.id;
         testdeliv16.product2id = deliv16.id;
         testdeliv16.IsActive = True;
         testdeliv16.UnitPrice = 10000;
         testdeliv16.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv16);
         
         PricebookEntry testdeliv17 = new PricebookEntry ();
         testdeliv17.pricebook2id = testpb.id;
         testdeliv17.product2id = deliv17.id;
         testdeliv17.IsActive = True;
         testdeliv17.UnitPrice = 10000;
         testdeliv17.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv17);
         
         PricebookEntry testdeliv18 = new PricebookEntry ();
         testdeliv18.pricebook2id = testpb.id;
         testdeliv18.product2id = deliv18.id;
         testdeliv18.IsActive = True;
         testdeliv18.UnitPrice = 10000;
         testdeliv18.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv18);
         
/*         PricebookEntry testdeliv19 = new PricebookEntry ();
         testdeliv19.pricebook2id = testpb.id;
         testdeliv19.product2id = deliv19.id;
         testdeliv19.IsActive = True;
         testdeliv19.UnitPrice = 10000;
         testdeliv19.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv19);
         
         PricebookEntry testdeliv20 = new PricebookEntry ();
         testdeliv20.pricebook2id = testpb.id;
         testdeliv20.product2id = deliv20.id;
         testdeliv20.IsActive = True;
         testdeliv20.UnitPrice = 10000;
         testdeliv20.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv20);*/

         Insert InsertPricebookList;
         

test.starttest();

        List<OpportunityLineItem> oli1 = new List<OpportunityLineItem>();
            integer todo = 20;
            for(integer bi=0; bi<todo; bi++) {
        
            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv1.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv2.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv3.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv4.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv5.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv6.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv7.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv8.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv9.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv10.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv11.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv12.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv13.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv14.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv15.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv16.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv17.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv18.id, OpportunityId = Opp.id) );

/*            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv19.id, OpportunityId = Opp.id, Prod_Grp__c = 'International - Platform') );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv20.id, OpportunityId = Opp.id, Prod_Grp__c = 'International - Coaching') );*/
         }
    
        insert oli1;
        
        delete oli1;

test.stoptest();
    }
}




Best Answer chosen by JN22
JN22JN22
Thanks for all the suggestions, but I figured it out.  I had 2 triggers on OpportunityLineItems.  One was a before trigger the othe an after trigger and they were getting caught in a loop.  I added some code to prevent recursive firings and was able to deploy to production!

All Answers

Denis VakulishinDenis Vakulishin
HI,
for(OpportunityLineItem oli:Trigger.new){
            setOliIds.add(oli.Id);
 }

And further in trigger, htere is one problem. 
In "Before Insert" trigger oli.Id will be NULL. So maybe this is the problem.

James LoghryJames Loghry
The reason you are seeing differences in test results between Sandbox and Production, is because you are relying on "SeeAllData=true" in your unit test.  The best practice is to stay away from SeeAllData=true, and instead create all your data in your test class.

Fellow Force.com MVP Matt Lacey has a blog post on this topic here: http://www.laceysnr.com/2012/04/seealldata-why-i-think-you-shouldnt-use.html
James LoghryJames Loghry
In regards to the SeeAllData, looks like you have PriceBookEntry functionality commented out, so that's probably why you added the annotation in the first place.  However, as of Summer '14 you can create PBs and PBEntries in unit tests, so you may want to look into that as well.
JN22JN22
Thanks.  When I remove the SeeAllData and uncomment the pricebook section (which was a mistake when I posted here but not done in my sandbox) my test now fails with the following error:

System.QueryException: List has no rows for assignment to SObject
Class.TestOppUpdates.TestOppUpdates: line 132, column 1

Which is line 144 in the above code.  Any idea why that might be happening now?
Denis VakulishinDenis Vakulishin
Check you apex code version. It must be 31.
JN22JN22
Denis,  Thanks.  My code was Version 27 but even with changing that I still get the same error when I try to deploy.
JN22JN22
Thanks for all the suggestions, but I figured it out.  I had 2 triggers on OpportunityLineItems.  One was a before trigger the othe an after trigger and they were getting caught in a loop.  I added some code to prevent recursive firings and was able to deploy to production!
This was selected as the best answer