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
Varshini DVarshini D 

Clone Child record to another master record using Custom Button

I have 2 objects Parent and Child. I have a Checkbox field called active in parent. Only one of the parent is active. I want to clone the active parent's child to new Parent using custom button.
 I wrote a class for cloning child for new Parent.
Code:

      
    public class TestCLoneChild {
       // public Parent__c parent{Get;Set;}
        public List<Child__c> ChildSO{get;Set;}
        public void getparent(Parent__c parent){
                Parent__c versionSO = [SELECT Id,(SELECT ID,Name,Data_Type__c FROM Child__r) FROM Parent__c WHERE Active__c = TRUE Limit 1];
            ChildSO= new List<Child__c>();
             for(Child__c attr : versionSO.Child__r){
                  Child__c childd= new Child__c();
                  childd.Name = attr.Name;
                  childd.Data_Type__c = attr.Data_Type__c;
                  childd.Version__c = version.Id;
                  ChildSO.add(childd);
                 } 
              upsert ChildSO;
              System.Debug('Child'+ChildSO);
            }
        }
How can call this class to clone Child Records for new parent.
Please suggest me some ideas.

Thanks in Advance.
Best Answer chosen by Varshini D
EuphoristeEuphoriste
Hi sorry for the late answer,

So for creating the flow : 

Select "New Flow" - > "Screen Flow"

1/ 
You need to start by creating the variable for getting the ID from the parent you are running the flow ( where you want to clone childs ) 
So for that, on the Toolbox, you go to "Manager" and create a new Ressource as below 

User-added image
This is Salesforce naming convention. If you create an Input Text variable with name "recordId", this variable will automatically be fulfilled with the id of the record where the flow is launched. ( here, the parent record ) 

2/ 
Second step, you need to get the ID of the Active parent for getting its childs on another step.
So for that, you select a "Get" Element and setup the criteria to get the active parent ( I selected "Store all fields " but you can choose to store only the id, you don't need other parent fields )
User-added image

3/ 
Third step, you need to get all active parent childs Collection.
So for that, once again you need to get elements but this time the criteria is "CHILD parent ID = ID of the get_Active_PARENT (previous get element ) 
And don't forgate to select "all records" because you want to store more than 1 child
User-added image

4/ 
Now that you get all CHILDS that you want to clone, you need to loop on each of it for creating them on your current Parent ( where you launched the flow ) 
For that you first need to create a new CHILD ressource. This CHILD will be a temporarly record using for iterating in your loop 
User-added image

Now you can setup your "Loop" Element : 

User-added image

So you loop throw your previous Get ( the Active Parent Childs collection ) using your temporarly variable created just before

And so now, the last step, for each iteration, you need to clone the child object : 
For that you use "Create record" Element : 

I reached maximum pics on my answer, i continue on my next message
 

All Answers

EuphoristeEuphoriste
Hello, 

Do you really need to do it using Apex ?

I am saying that because your request could be done with an auto-launched flow from action button. 
Using apex can work as well but as much as you can avoid it as much as consistant will be your org.

If you are not used with Salesforce flow I can help you.

Best regards.
Varshini DVarshini D
Hi Euphoriste,
          I am not aware of autolaunched flows. Can you help me? and I want to know how to apex call a class in custom button for future use.
Thanks in Advance.
EuphoristeEuphoriste
Hi sorry for the late answer,

So for creating the flow : 

Select "New Flow" - > "Screen Flow"

1/ 
You need to start by creating the variable for getting the ID from the parent you are running the flow ( where you want to clone childs ) 
So for that, on the Toolbox, you go to "Manager" and create a new Ressource as below 

User-added image
This is Salesforce naming convention. If you create an Input Text variable with name "recordId", this variable will automatically be fulfilled with the id of the record where the flow is launched. ( here, the parent record ) 

2/ 
Second step, you need to get the ID of the Active parent for getting its childs on another step.
So for that, you select a "Get" Element and setup the criteria to get the active parent ( I selected "Store all fields " but you can choose to store only the id, you don't need other parent fields )
User-added image

3/ 
Third step, you need to get all active parent childs Collection.
So for that, once again you need to get elements but this time the criteria is "CHILD parent ID = ID of the get_Active_PARENT (previous get element ) 
And don't forgate to select "all records" because you want to store more than 1 child
User-added image

4/ 
Now that you get all CHILDS that you want to clone, you need to loop on each of it for creating them on your current Parent ( where you launched the flow ) 
For that you first need to create a new CHILD ressource. This CHILD will be a temporarly record using for iterating in your loop 
User-added image

Now you can setup your "Loop" Element : 

User-added image

So you loop throw your previous Get ( the Active Parent Childs collection ) using your temporarly variable created just before

And so now, the last step, for each iteration, you need to clone the child object : 
For that you use "Create record" Element : 

I reached maximum pics on my answer, i continue on my next message
 
This was selected as the best answer
Varshini DVarshini D
Thanks Euphoriste.I will try this.
EuphoristeEuphoriste
Create record element : 

User-added image
Here the important line is to map the Child Parent ID with the recordId ( the ressource that you created on step 1 ) and all the fields that you want to clone 

Your flow should look as this : 
User-added image
Now you need to create the action on your PARENT object : 
User-added image
If you are using Salesforce classic you can call the flow by using URL
Add this action to your page layout and all should be ok !! 

If you have any questions feel free to ask me. 

And for your question about Apex Code, I don't have enough knowledge for answering you but i think you can launch VF page throw button and call your Apex class in it.
This salesforce help may help you : 
https://help.salesforce.com/articleView?id=000325250&type=1&mode=1