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
DarbDarb 

test code error - list has no rows for assignment to SObject

I am trying to deploy a change set from a sandbox and an existing trigger is preventing me from doing so.  The test does not have the required coverage.  The error I get is System.QueryException: List has no rows for assignment to SObject.  The test code is as follows:

@isTest
private class LeadTriggerTest {

    static testMethod void LeadTest() {
       String testleadid='';
       Lead testlead = new lead();
       testlead.status='new';
       testlead.lastname='Test';
       testlead.company='Test';
       testlead.description='test';
       testlead.country='United Kingdom';
       //get one user of proper type, use the where clause to filter
       user u=[select id,name from user where user.name='jonathan parker' LIMIT 1 ];
       system.runas(u){
         try{
            insert testlead;
            testlead.country='Norway';
            update testlead;
            testleadid=testlead.id;  //if created correctly
          }catch(DMLException e){
             system.assert(false, 'Error occurred inserting test record:'+e.getDMLMessage(0));
            }
          }
         Lead checkcase = [select id,name from lead where id=:testleadid];
         
         
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Andy BoettcherAndy Boettcher

You have to create that user in your test class first - by default test classes do not have any access to Production data, such as Users.

 

-Andy