+ Start a Discussion
FrankJSalinasFrankJSalinas 

@isTest - Querying for inserts, is there a difference between Sandbox and Production environments?


To jump right to the question, I have a test class that works great in our Sandbox. It uses a custom Data Factory class that creates all the data it needs. It simulates loading status updates to a load object called Loader_GE_Status_Update__c which then updates various opportunities based on rules. I create the Load data, insert it, then query the opportunity to verfy the rules updated it correctly. Like I said, it works great in the Sandbox. But trying to move it to production, the opportunity query always fails for "zero rows found". Based on googling this, I saw a post that said that queries against data in production and sandboxes work differently. I would find that hard to believe, but does anyone know if it is different?
Here's some more information on what I'm doing:
I created a data factory class to create test data for my test methods. It's called TestDataFactory.  I created a Test Trigger class for an object called Loader_GE_Status_Update__c. This object is a loader for new statuses to opportunities.  The Test Trigger needs a few Accounts and Opportunities created before I use the trigger, then it inserts a load of Status updates through the Loader_GE_Status_Update__c object. The load object has rules that determine what Opportunity fields get updated. My test is to verify that the opportunity. After the insert, I check if the opportunity to see if it got updated corrected. I do that by querying the opportunity to see if the update happened. In EQA, this test class works great. I see the field was updated. But in Production, it fails validation because the query always returns no rows. A co-worker says that googling shows that Production test methods behave differently in Production than they do in Sandboxes. Is that what is going on?  I can't think of any other reason this is failing in production

    static testMethod void myUnitTestCancelled() {
        // TO DO: implement unit test
        system.debug('myUnitTestCancelled being executed');
        List<String> visitNumbers = new List<String>();
        List<String> encounterNumbers = new List<String>();
        List<Opportunity> myOpps = testDataFactory.dfacCreateOpportunityVisitsWithAccounts (5, 
               'SHIP', 'Customer', 'New', 500000, 700000);
        //capture the Visits and Encounters to create a Load list       
        for ( Opportunity myOpps1: myOpps ) {
         visitNumbers.add(myOpps1.VisitNumber__c);
         encounterNumbers.add(myOpps1.Encounter_Number__c);
        }
        system.debug('myUnitTestCancelled visitNumbers.size()'+visitNumbers.size());
        //Create the Load from GE
        List<Loader_GE_Status_Update__c> loadCxd = testDataFactory.dfacCreateGEStatusLoad(5, 'CANCELLED', 
            'DUPLICATE ORDER', visitNumbers, encounterNumbers);
        test.startTest();
        insert loadCxd; <-- Here's the INSERT - based on rules, it should update the opportunity fields with new statuses
        test.stopTest();
        Opportunity opp = [Select VisitStatus__c From Opportunity Where VisitNumber__c in :visitNumbers LIMIT 1]; <-- This is always zero in production
        system.assertEquals( opp.VisitStatus__c, 'CANCELED' );
        
    }

This is the error (happens only in production):
System.QueryException: List has no rows for assignment to SObject
Stack Trace: Class.TestLoader_GE_Status_Update_AfterInsert.myUnitTestCancelled: line 44, column 1
System.QueryException: List has no rows for assignment to SObject
Stack Trace: Class.TestLoader_GE_Status_Update_AfterInsert.myUnitTestDealerShipped: line 100, column 1

    
Can someone give me insight as to why there is a difference between Sandbox and production.
Thanks!
Naveen IlaNaveen Ila
Does data in production and sandboxes work differently? 
Ans : No. Production and Sandboxes works in same manner. 

I think you might missed some configuration checks which updates Opportunity by the trigger on "Loader_GE_Status_Update__c".  

  Opportunity opp =  [Select VisitStatus__c From Opportunity Where VisitNumber__c in :visitNumbers LIMIT 1]; 
Just to make a note always assign SOQL result to a list not to a Sobject. Write debug logs in trigger and check the flow once again in production.


If possible can you share the code.
Email : naveenila@gmail.com  

 
FrankJSalinasFrankJSalinas
Hi Naveen,

I changed the code to change it to a LIST, but it still fails with the same error. I'll debug.  I don't think you can debug in a deployment since it doesn't exists until it is successfully deployed.  I just modified it to make sure it passes, and it is in production now.