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
vasu takasivasu takasi 

test method for SOQL

hi i have a query i n my controller as DataLoadTest__c selectedRecord = [select Id,name,city__c,country__c,phone__c from DataLoadTest__c where id=:rId limit 1]; how to write test method for this query. it showing exception as System.QueryException: List has no rows for assignment to SObject
bob_buzzardbob_buzzard

You'll need to create a record that satisfies the criteria as part of your test.

Navatar_DbSupNavatar_DbSup

HI,


You have to simply insert a record DataLoadTest__c inside the test method before calling that function.


DataLoadTest__c d=new DataLoadTest__c(name=’test’, other mendatory fields);
Insert d;

 

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

vasu takasivasu takasi

i created a record, but still its showing same exception

vasu takasivasu takasi

i created a record, but still its showing same exception

bob_buzzardbob_buzzard

Did you create the record as part of the test, or did you create it in the org (e.g. via the UI)?

stunaitestunaite

Y have a where clause... remove it from query or ensure that the criteria is satisfied with the record y created.... otherwise it will return no rows

 

if you do like below it will return a record

 

 DataLoadTest__c yourRecord =  new DataLoadTest__c();

 Id rId = yourRecord.id

 DataLoadTest__c selectedRecord = [select Id,name,city__c,country__c,phone__c from DataLoadTest__c where id=:rId limit 1];

vasu takasivasu takasi

i created in test method also.

VasanthKumarVasanthKumar

Can you please copy your exact class here, so we can give the exact code.

vasu takasivasu takasi
public with sharing class CloneTestingRecordClass {
public string roid{get;set;}
    public PageReference dodelte() {
    dataloadtest__c dt=new dataloadtest__c();
    dt=[select id from dataloadtest__c where id=:roid];
    delete dt;
    return null;
        
    }
    public PageReference clonedRec() {
        DataLoadTest__c selectedRecord = [select Id,name,city__c,country__c,phone__c from DataLoadTest__c where id=:rId limit 1];
        dataLoadtest__c lstnew = selectedRecord.clone(false);
        insert lstnew;
        
       
        return null;
    }
    public String rId{get;set;}
    
    List<DataLoadTest__c> lstdlt = new List<DataLoadTest__c>();
    public List<DataLoadTest__c> getRecords() {
        lstdlt = [select Id,name,city__c,country__c,phone__c from DataloadTest__c];
        return lstdlt;
    }
public static testmethod void testname1()
{
 CloneTestingRecordClass ctr=new CloneTestingRecordClass ();
 
 dataloadtest__c dt=new dataloadtest__c ();
 dt.name='taj';
   insert dt;
  string roid=dt.id;
 // ctr.clonedRec();
  ctr.getRecords();
  ctr.dodelte();
 
}
}
bob_buzzardbob_buzzard

You haven't set the record id into the controller.  You also seem to have roid and rid in the controller - I'm guessing its the rid that needs to be set up but I could be wrong:

 

  string roid=dt.id;
  ctr.ri=roid;
 // ctr.clonedRec();
  ctr.getRecords();
  ctr.dodelte();

 

ShailforceShailforce
@isTest(seeAlldata=true)
private class TestClass {

}