1

I have a requirement where a related list records needs to be duplicated and put in another related list where both are related to the same parent. Has anyone built it?

  • 1
    Are you trying to build it on a Visualforce Page? – Adrian Larson Feb 8 '17 at 11:16
  • Looking for ideas to build this actually. Can this be done by having a custom button on the first related list and on it's click, the records under them are cloned to put them on the other related list? @AdrianLarson – Sowparnika Feb 8 '17 at 11:41
  • Its possible and quite common functionality, what have you tried so far? – Raul Feb 8 '17 at 12:11
  • The parent being opportunity, I wanted to clone the opportunity under each opportunity and insert those records to another custom object which is in a lookup relationship with Opportunity object. @RahulSharma – Sowparnika Feb 8 '17 at 14:00
0

Assuming you are trying to do this in Apex, here is how you can accomplish this. First Query all the child records, clone them and add them to a List. Insert the List so that it will create another related list.

String parentId = Parent.Id;
// Query for all Child records  
String query = String.format(
    'SELECT {0} FROM {1} WHERE Claim__c = \'\'{2}\'\'',
    new String[] {
        String.join(
            new List<String>(
                Child__c.SObjectType.getDescribe().fields.getMap().keySet()
            ),
            ','
        ),
        String.valueOf(Child__c.SObjectType),
        parentId
    }
);
List<Child__c> children = new List<Child__c>();
try{
    for (Child__c child:(List<Child__c>)Database.query(query)) {
        children.add(child.clone(false,true,false,false));
    }
    if (!children.isEmpty()) {
        System.debug('Child List  successfully cloned!');
    }
    for (Child__c child : children) {
        child.Claim__c = Parent.Id;
    }
    //Insert the Child Records
    Database.insert(children);                
}catch(Exception ex){
    System.debug(ex.getMessage());
}
| improve this answer | |

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.