i am new to apex...

My Apex Class..

public class StudentLists {

    public List<Student__c> stuList{get;set;}

    public String name1 {get;set;}
    public String eid {get;set;}

    Public StudentLists(){

        stuList= new List<Student__c>();
        stuList= [select name,Address__c,phone__c,email__c,zipcode__c from Student__c ];

    public void deleteAll(){

        List<Student__c> studentDeleteAll = new List<Student__c>();
        studentDeleteAll = [SELECT name,Address__c,phone__c from Student__c ];
        delete studentDeleteAll;

    public PageReference  new1(){

        PageReference oPageRef = Page.sturecord;
        return oPageRef;

    public pageReference edit(){

        PageReference oPageRef = Page.stuEdits;
        return oPageRef;

    public pageReference deleteOneRecord(){

        List<Student__c> studentDelete = new List<Student__c>();
        System.debug('Name: '+name1);
        studentDelete = [SELECT name, Address__c, phone__c FROM Student__c WHERE name=:name1];
        delete studentDelete;
        PageReference pageRef=Page.StudentList;
        return pageRef;

here my test class of deleteOneRecord....

    static void testDelete(){

        StudentLists studentList = new StudentLists();
        String beforeDeleteName = [SELECT name FROM student__c ORDER BY name DESC LIMIT 1].name;
        String afterDeleteName = [SELECT name FROM student__c ].name;
        System.assertNotEquals(beforeDeleteName, afterDeleteName);

How to solve this exception in test class:System.QueryException: List has no rows for assignment to SObject...

deleteOneRecord Work Properly.

2 Answers 2


You have no student records in your test method. You need to create and insert one student record or your query will return 0 items and fail.

You can make your code more robust by saving the query into a list and checking that you have at least one record.


You get this error when you try to assign the result of a query (which returns no records) to a single instance of a specific sObject, like this...

// Assuming here the query didn't return any rows.
CustomObject customObjectInstance = [SELECT Id, Name FROM CustomObject__c WHERE Name = NULL];

Instead, preferably perform the same as follows...

// Assuming here the query didn't return any rows.
List<CustomObject> lstCustomObjects = [SELECT Id, Name FROM CustomObject__c WHERE Name = NULL];
CustomObject customObjectInstance;
if(lstCustomObjects != NULL && lstCustomObjects.isEmpty() == false) {
    customObjectInstance = lstCustomObjects[0];

Regarding the issue

Once you delete the record using your custom method, as it is a test class and you don't have access to the data that exists in the org, the query will return no records. Thus you will not be able to put the asserts as you have now.

You can simply put assert on the size of the list returned by the query (which should be 0).

Hope this helps.

Best Regards!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .