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
SFDC_2706SFDC_2706 

Merge custom object records with the child records..!!

Hi
I want to merge custom object records with all the childs realted to those records. There is one parent object and it has 6 child objects. Now i want to merge the parent records.
Example :
- If one parent has 3 child records(3 different objects) and another parent has 3 child records(3 different objects) and if i merge these two parents then the merged record should have total 6 child records with 1 record in every child.
- If one parent has 3 child records(3 different objects) and another parent has 3 child records(same 3 objects) and if i merge these two parents then the merged record should have total 6 child records with each child has 2 records.

if someone has implemented this functionality then please let me know ASAP as I need to complete this on very urgent basis.

Your help would be greatly apprecaited. Thanks in advance..!!
Pradeep SinghPradeep Singh
Hi ,
Please refer below code:-
You have to do little bit enhancement here.
---------------------------------------------------------------------------------------------
<apex:page controller="CustomMerge">
<apex:form>
    <apex:pageBlock>
        <apex:commandButton value="merge" action="{!mergeRecords}"/>
        <apex:pageBlockSection>
            <apex:inputField value="{!childrecord.Parent__c}" label="Merge Into"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>    
</apex:page>

​----------------------------------------------------------------------------------------------------
public class CustomMerge{
    
    public MergeObj__c childRecord{get;set;}
    public List<Batch__c> btList = new List<Batch__c>();
	public List<Batch2__c> bt2List = new List<Batch2__c>();
    
    public CustomMerge(){        
        childRecord = [Select id,name,Parent__c,(select id,name,MergeObj__c from Batches__r),(select id,name,MergeObj__c from Batches2__r) from MergeObj__c where id =: apexpages.currentpage().getparameters().get('id')];
    }
    
    public void mergeRecords(){
        if(childRecord != Null){
            MergeObj__c parentRecord = [Select id,name from MergeObj__c WHERE id=: childRecord.Parent__c];
            
            for(Batch__c bt : childRecord.Batches__r){
                bt.MergeObj__c = parentRecord.id ;
                btList.add(bt);
            } 
			
			for(Batch2__c bt : childRecord.Batches2__r){
                bt.MergeObj__c = parentRecord.id ;
                bt2List.add(bt);
            }
            
            update btList ;
			update bt2List ;
            
            delete childRecord ; 
        }   
    }
}

Hope this helps you.

If this helps you, please mark it as solved