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
LoguLogu 

System.QueryException: List has no rows for assignment to SObject

HI,
 I am new to salesforce, Can any one help me to fix this issue. I am trying to write test class for updating the record using standard controller, and extension .I am getting exception in objAccount , which throws System.QueryException: List has no rows for assignment to SObject.

      I can get the results in query editor by executing 'select name, owner.name, site ,id, Industry from account where id = '0019000001FYa8GAAT' ' this query ,
       
Account objAccount = [select name, owner.name, site ,id, Industry from account where id = '0019000001FYa8GAAT' ];
ApexPages.StandardController sc = new ApexPages.standardController(objAccount);
myPageCon = new AccountEditPageCon(sc);

Please help me to fix this issue. Thanks in advance
 
Best Answer chosen by Logu
AMIT KAMBOJAMIT KAMBOJ
Hi Logan,

Please add this in first line of your test class. Please mark this as answer as solution if that resolves your issue.

@isTest(SeeAllData=true)

e.g.
@isTest(SeeAllData=true)
public class TestDataAccessClass
{

}
 

All Answers

Himanshu ParasharHimanshu Parashar
The Issuse seems like you are not creating test data inside your test class and in your controller you are doing query.

Please post your class and Test class code so that we can tell you corrections instead of writing everything from scratch.

Thanks,
Himanshu
Justin RuckJustin Ruck
What I usually do is create a new account in the test class and then update it.  For example (insert your own info here):
 
//define where your trigger is and define a new trigger instance
AccountUpdate acctUpdate = new AccountUpdate ();

//create new account
Account newacct = new Account();
        newacct.Name = 'testsalespipe';
        newacct.Account_Type__c = 'Other';
        insert newacct;

//update account
newacct.Account_Type__c = 'Downstream';
update newaccount;

//get list
List<Account> acctlist = acctUpdate.UpdateAccount (newaccount);
System.assertEquals (1, acctlist.size());

//this should pull back the Account record.  Make sure that all of your required fields for the Account are filled in here.

 
AMIT KAMBOJAMIT KAMBOJ
Hi Logan,

Please add this in first line of your test class. Please mark this as answer as solution if that resolves your issue.

@isTest(SeeAllData=true)

e.g.
@isTest(SeeAllData=true)
public class TestDataAccessClass
{

}
 
This was selected as the best answer