function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
krishna guptakrishna gupta 

Clone parent-->child-->grand child

Hi All,

I have a requirement where i have to clone parent, their child and for each child their subchild.
Can onyone let me know how to process this requirement?
Abhishek Raj 13Abhishek Raj 13
Hi Krishna,

You can write a script n run that scriot from developer console. That script will contain a method which takes argument of parent record id. Using that Id, you can use inner query n get all the records related to parent. like child and grand child. Once you have all the related records, you can insert all of them to DB.Your recordss would be cloned.

Please let me know in case of any concerns.

Abhishek Singh 88Abhishek Singh 88
hi try this code
public class ClonePlusController {

  public List<relatedObjects> objectChildren  { get; set; }
  public String    objectTypeName  { get; set; }
  public String    objectName      { get; set; }
  private SObject headSObject, headClone;
  // Initialization method called when the clone plus page is  loaded.
  // Use the id page parameter to find out what object type to clone.
  // Then load the object from the database.
  // Finally call the populateObjectChildren method to      
  public void initialiseObjectsForCloning()

    // Here we generate a keyprefixmap using the global describe 
    // Then compare that to our object to determine type.  
    Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
    Map<String,String> keyPrefixMap = new Map<String,String>{};
    for(String sObj : gd.keySet()){
      Schema.DescribeSObjectResult r =  gd.get(sObj).getDescribe();
      keyPrefixMap.put(r.getKeyPrefix(), r.getName());
    String objectID = ApexPages.currentPage().getParameters().get('id');
    String objectTypeKey = objectId.subString(0,3);
    objectTypeName = keyPrefixMap.get(objectTypeKey);
    String primaryObjectQueryString = 'SELECT Id, Name FROM '
                                    + objectTypeName
                                    + ' WHERE Id = \''
                                    + objectId
                                    + '\'';
    headSObject = Database.query(primaryObjectQueryString);
    objectName          = '' + headSObject.get('Name');

  // Get all of the children of the current object that have a object
  // type contained in the child object types page parameter.
  // Not restricting the child objects to particular types results in
  // many uncloneable system objects being added to the possibiilites.
  // Making these object type choices also allows us 
  // pick and chose the specific kinds of objects we want to clone.  
  public void populateObjectChildren()
    objectChildren = new List<relatedObjects>{};
    Set<String> childObjectTypes = new Set<String>{};
    // read the object types from the page parameter.    

mark as best answer if it works to keep community clean..

Vedanth VadlakondaVedanth Vadlakonda
Hi Abhishek Singh 88, Can you help me with the test class for ClonePlusController.