We already have the function deep clone to clone the fields. It has been done via SOQL query(i.e selected fields only cloned).
We need a dynamic clone of the objects without querying the field. Please refer the below
public class DynamicClone { public static List<sObject> cloneObjects(List<sObject> sObjects, Schema.SObjectType objectType){ // A list of IDs representing the objects to clone List<Id> sObjectIds = new List<Id>{}; // A list of fields for the sObject being cloned List<String> sObjectFields = new List<String>{}; // A list of new cloned sObjects List<sObject> clonedSObjects = new List<sObject>{}; // Get all the fields from the selected object type using // the get describe method on the object type. if(objectType != null){ sObjectFields.addAll( objectType.getDescribe().fields.getMap().keySet()); } // If there are no objects sent into the method, // then return an empty list if (sObjects != null && !sObjects.isEmpty() && !sObjectFields.isEmpty()){ // Strip down the objects to just a list of Ids. for (sObject objectInstance: sObjects){ sObjectIds.add(objectInstance.Id); } /* Using the list of sObject IDs and the object type, we can construct a string based SOQL query to retrieve the field values of all the objects.*/ String allSObjectFieldsQuery = 'SELECT ' + sObjectFields.get(0); for (Integer i=1 ; i < sObjectFields.size() ; i++){ allSObjectFieldsQuery += ', ' + sObjectFields.get(i); } allSObjectFieldsQuery+='FROM'+objectType.getDescribe().getName()+ 'WHERE ID IN (\''+sObjectIds.get(0)+ '\''; for (Integer i=1 ; i < sObjectIds.size() ; i++){ allSObjectFieldsQuery += ', \'' + sObjectIds.get(i) + '\''; } allSObjectFieldsQuery += ')'; try{ // Execute the query. For every result returned, // use the clone method on the generic sObject // and add to the collection of cloned objects for (SObject sObjectFromDatabase: Database.query(allSObjectFieldsQuery)){ clonedSObjects.add(sObjectFromDatabase.clone(false,true)); } } catch (exception e){ // Write exception capture method // relevant to your organisation. // Debug message, Apex page message or // generated email are all recommended options. } } // return the cloned sObject collection. return clonedSObjects; } } An example of how you would call this method to clone an Opportunity object from another Apex controller is: Account ExisitingOpportunity= [select Id from Opportunity where id=:ApexPages.currentPage().getParameters().
get
(
'id'
)
]; sObject originalSObject = (sObject)ExisitingOpportunity; List<sObject> originalSObjects = new List<sObject>{originalSObject}; List<sObject> clonedSObjects =DynamicClone .cloneObjects( originalSobjects, originalSobject.getsObjectType()); Opportunity clonedOpportunity = (Opportunity)clonedSObjects.get(0);
We are the ISV Partners and Please reach us for custom development => www.merfantz.com
------------------------Thank You-------------------------