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 Answer
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());
}
Visualforce Page
?