+ Start a Discussion
mstarksmstarks 

Test class is no longer recognizing an inserted test Account record

In a test class, create an Account with a unique name.  Next, create an Id variable and attempt assigning the variable by selecting the Account by the unique name.  Error returns - list has no rows for assignment to sObject.
Best Answer chosen by mstarks
SvenfSvenf
Update: Salesforce deployed the patch and our issue has been resolved.

All Answers

CloudGeekCloudGeek
Hello Mstarks,

Try something like below ans see if that helps :
 
@isTest(SeeAllData = true)
public class Your_TestClass_Name {

  public static testMethod void testForSample(){
        
    Test.startTest();

    Account account = new Account(Name = 'Hello World');
        insert account;

       //To Get the ID from Account Created Above
    
       Id myAccountID = account.Id;

      //OR You can try like below
      
      String myAccID = account.Id;
       
    Test.stopTest();  
 }
}


Hope that helps!
mstarksmstarks
Thanks for the reply.  I am able to get the Id as you show above, however the issue is when the main class which is being tested tries to select the Account from the test data.  The account record is no longer being found as it had been last week (with no changes to the main or test class.)  The account record appears to disappear after it is inserted in the test class.
CloudGeekCloudGeek
ahhh , I got it . You have some other Test Class which is where you are trying to refer to this created account record ?

One thing to notice is that, for Test Classes, whatever the data records we create will not be stored in database, but you can only refer to them in present context in test class.


Note: Mark this as solution if that helps.
 
mstarksmstarks
That's correct.  We keep our test classes in different files.  This is happening in the present context.  I just now attempted to Clear All Test Data as a possible solution and for the first time received an error saying the test data could not be cleared.
CloudGeekCloudGeek
Okay.

Try something like below :
 
public class TestDataCreator {


public static List<Account> testAccounts;

 public static  void testAccountCreator()
    {
        testAccounts = new List<Account>();
    
        Account acc1 = new Account(Name = 'Hello World');
        
        testAccounts.add(acc1);

    }

}

Now in your TEST Class:
 
@isTest(SeeAllData = true)
public class UsingTestDataCreator_Class {

  public static testMethod void testForSample(){
        
    Test.startTest();
    
    TestDataCreator.testAccountCreator();  //invoke this to create account and add created account to list to use
    
    String accountID = TestDataCreator.testAccounts.get(0).Id;  //Access the List using ClassName.method() syntax
    
    System.debug('accountID = '+accountID+ 'Account Name = '+TestDataCreator.testAccounts.get(0).Name);
       
    Test.stopTest();  
 }
}


Hope this help!
 
mstarksmstarks
Thanks for the reply.  Using the method above also results in same issue unfortunately.  I've also tried creating the test account record using this method:

@testSetup static void setup()

Each time when the class being tested attempts to select the inserted test account record, it is not found.  This is happening across any classes using Account records in test classes.  Other types of records (both standard and custom) are selected without a problem.  Strange.
CloudGeekCloudGeek
Can you try to post your class code and also the test class ?

I would like to take a look at it and see if I could investigate further.
SvenfSvenf
Hi,

I actually encounter the same issue with a custom object in a full sandbox after it was upgraded to Summer 16. It must be related to the Summer 16 release, because all the tests are being executed every night via Jenkins and they only started to fail after the upgrade.

Here is one of the tests that is failing now. 
static testMethod void checkOrderStatusMappingInsert() 
    {  
        Test.startTest();
        Service_Order__c serviceOrder = cpTestFactory.getTestServiceOrder();
        serviceOrder.SAP_STATUS__c = 'SAP STATUS';
        insert serviceOrder; 
        Test.stopTest();
        
        serviceOrder = [SELECT Order_Status__c FROM Service_Order__c WHERE Id =: serviceOrder.Id];
        System.assertEquals('SAP STATUS', serviceOrder.Order_Status__c);
    }

The query returns List has no rows for assignment to SObject. When I add "ALL ROWS" to the query, it seems to find the record, but the isDeleted flag indicates false. There is also no delete logic for the object, so that also can't be the cause.

It appears that when I add certain fields to the SELECT section, it will return the record. The below queries all result in the error.
[SELECT Id FROM Service_Order__c WHERE Id =: serviceOrder.Id]
[SELECT Name FROM Service_Order__c WHERE Id =: serviceOrder.Id]
[SELECT Id, Order_Status__c FROM Service_Order__c WHERE Id =: serviceOrder.Id]
[SELECT Name, Order_Status__c FROM Service_Order__c WHERE Id =: serviceOrder.Id]
[SELECT Id, Name FROM Service_Order__c WHERE Id =: serviceOrder.Id]

For instance the field Action_Code__c seems to be a non-problematic field, so the following queries will return the record without any issue.
[SELECT Action_Code__c FROM Service_Order__c WHERE Id =: serviceOrder.Id]
[SELECT Action_Code__c, Order_Status__c FROM Service_Order__c WHERE Id =: serviceOrder.Id]
[SELECT Action_Code__c, Id FROM Service_Order__c WHERE Id =: serviceOrder.Id]
[SELECT Action_Code__c, Name FROM Service_Order__c WHERE Id =: serviceOrder.Id]
[SELECT Action_Code__c, Id, Name FROM Service_Order__c WHERE Id =: serviceOrder.Id]

Thanks for any help in advance.
mstarksmstarks
Thanks Sven.  That sounds identical to the issue we are having.  I'll try adding additional fields to the select statement.  How do we raise this issue wtih Salesforce?
mstarksmstarks
Thanks for the assistance CloudGeek.

Here is sample code that produces issue for us:

Account a = new Account();
           
a.Name = 'Test';
a.Global_Region__c = 'North America';
a.Global_Sub_Region__c = 'SMB1';
a.BillingState = 'OH';
           
insert a;

Id acctId = [select Id from Account where Name = 'Test'].Id;
SvenfSvenf
I logged a case with Premier Support. Someone from the Developer Support team had a look at the issue and afterwards he escalated it to a higher level of support to investigate the issue further.
mstarksmstarks
Thanks for the update Sven.  Could you please keep me posted on any progress?  This is severely affecting our development processes.  Thanks.
SvenfSvenf
Yes, I will keep you updated. I also pointed them to this topic. Support is checking the issue on the database level at the moment. There is no further info about this so far.
SvenfSvenf
There is a skinny table enabled for the custom object. According to support the issue might be resolved when they disable the skinny table. R&D keeps searching for a solution, so I told support that I will wait for another solution.

mstarks, do you have a skinny table enabled for the Account object?
mstarksmstarks
Thanks for the update Sven.  We are not using skinny tables.  I was not aware they existed until now - and it looks like you have to work with Support to request them.  It also appears they are not utlized in the sandbox which is where we're experiencing the issue.

https://developer.salesforce.com/blogs/engineering/2013/03/long-and-short-term-approaches-for-tuning-force-com-performance.html
mstarksmstarks
Update:  This issue is occuring for us on our full sandbox on cs27.  The issue is not occurring in our dev org on cs14.
mstarksmstarks
Sven - what instance are you on?  Any updates from support?  Thanks.
mstarksmstarks
Update:  Issue does not occur on CS24 or CS16 where we just refreshed a partial and a dev org.  They are both on Spring 16.  Issue still occurring on CS27 on Summer 16.
SvenfSvenf
Hi Mike,

Our full sandbox is on cs87. In our case it does seem to be related to the skinny tables according to support. They recognized it as a known issue: https://success.salesforce.com/issues_view?id=a1p3A000000IXfz They expect to push the patch around the middle of next week. 
mstarksmstarks
Thanks for the info Sven.  Will check out things next week and update.
SvenfSvenf
Update: Salesforce deployed the patch and our issue has been resolved.
This was selected as the best answer
mstarksmstarks
Thanks for the update Sven.  Our issue appears to be resolved as well (even though we are not using skinny tables.)