0

I an encountering the following error in my Auraenabled apex class:

List has no rows for assignment to SObject

I am aware that this occurs if the query doesn't return any rows, so I included a null check just to be sure. But upon checking, same error occurs.

How can I fix this?

Controller:

public with sharing class CustomObjectController {
    
    @AuraEnabled
    public static CustomObject__c cloneCustomObject(CustomObject__c custObjRec){
        
        String customObjId;
        CustomObject__c customObjClone = new CustomObject__c();
       
        custObjRec = [SELECT Id FROM CustomObject__c WHERE Id =: customObjId];
        
        if(custObjRec != null) {

            customObjClone = custObjRec(false, false, false, false);
            insert customObjClone; 
        }
        
        return customObjClone;
    }
   
}

2 Answers 2

1

There are several fundamental issues with this code

  • You do not initialize customObjId, so your query becomes (effectively) [SELECT Id FROM CustomObject__c WHERE Id = null]
  • Assigning the result of a query to a single SObject instance only works if the query returns exactly one row ("no rows for assignment" or "more than one row for assignment" otherwise)
  • To clone an object, you need to call the clone() method. E.g. MyObject__c obj = otherMyObject.clone(false, false, false, false);
  • Cloning an SObject only clones the fields of the in-memory instance of the object, not all of the data stored in the database (we use queries to get data from the database, and make it available in-memory). What you have right now is effectively cloneRec = new Record__c();
  • Unless you're fetching more fields from the database, there's no reason to query at all

All that said, your current code could be entirely replaced with the following, and still give you the same result:

@AuraEnabled
public static CustomObject__c cloneCustomObject(CustomObject__c custObjRec){
    return new CustomObject__c();
}

If you do want to actually clone the record, some pseudo-code to get you started:

@AuraEnabled
public static CustomObject__c cloneCustomObject(CustomObject__c custObjRec){
    check if input is null
        if null, either return null, throw an exception, or return a new instance

    /**
     * If you need to clone more fields than what you get from your input record,
     *   then implement this in addition to the uncommented stuff (and implement it
     *   here, between the two uncommented sections)
     * - run a query gathering all the fields you want to clone
     * - store the result in a List
     * - check if the list is empty (return null, throw an exception, or return a new instance)
     * - if not empty, pull the first record (index 0) from the list
     */

    use .clone() to clone the input record
    return the cloned record

}
0

As the error message suggests - List has no rows for assignment to SObject. It is not at NULL check. It is at the assignment.

Here in the below code, it is not having value for the customObjId. So it will not return any rows.

String customObjId;
CustomObject__c customObjClone = new CustomObject__c();
custObjRec = [SELECT Id FROM CustomObject__c WHERE Id =: customObjId];
        

You must log in to answer this question.

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