0

In my following AuraEnabled class, I am trying to clone the current record displayed in my lightning component page. This lightning component implements force:hasRecordId and should insert it as new value instead of updating the current one.

But upon checking the developer console's debug logs, I always see this error:

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

What should I do to fix said error?

ItemsController.cls

public with sharing class ItemsController {
    
    @AuraEnabled
    public static Item__c cloneItem(Id itemId){
        
        Item__c itemToClone = new Item__c();

        itemToClone = [SELECT Id, MasterField__c FROM Item__c WHERE Id =: itemId LIMIT 1];

        Item__c newCloneItem = new Item__c();
        newCloneItem = itemToClone.clone();
        newCloneItem.MasterField__c = itemToClone.MasterField__c;
        insert newCloneItem; 
       
        return newCloneItem;
    }
   
}
1
  • Can you please share code of lightning component.
    – Kritik
    Nov 3 '20 at 4:30
3

Presumably, you're querying the wrong sObject, the recordId attribute was not pulled from the component state correctly, or the component was not a top-level component and therefore did not automatically get the recordId, despite implementing force:hasRecordId (only top-level components automatically get the ID, all others must have it passed in from a parent component). Without seeing more code, there's not much else we can offer to you. All I know for certain is that the error means no results were returned from the query, so the ID value was either the wrong type or a null value.


P.S. Your code is incredibly overengineered:

public with sharing class ItemsController {
    @AuraEnabled
    public static Item__c cloneItem(Id itemId){
        Item__c newCloneItem = [SELECT MasterField__c FROM Item__c WHERE Id = :itemId].clone(false, false, false);
        insert newCloneItem; 
        return newCloneItem;
    }
   
}
1
  • I see, maybe I overlooked the lightning component. Gonna check on that part also. Thanks!
    – Ertyq
    Nov 3 '20 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.