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
}