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
pokalakari nagapokalakari naga 

How to show the parent object fields in the same page

Hi all,

I was trying to create the custom CLONE button which clone the child object records also. For this I have used a custom vfpage and custom controller.
I want to show the child object fileds with in the same page in the edit mode. so that we can edit the values of the child record before cloneing the record.
 
<apex:page controller="ClonePlusController" action="{!initialiseObjectsForCloning}">
  
  <apex:sectionHeader title="Clone Plus: {!objectName}"/>

  <apex:form id="theform" >
   
    Please select the child objects you would like to clone.<br/><br/>
  
    <apex:repeat value="{!objectChildren}" var="child">
      <apex:PageBlock title="{!child.pluralLabel}"> 
        <apex:pageBlockTable value="{!child.objectRows}" 
                             var="objectRow">
          <apex:column headerValue="Clone" width="10%">
            <apex:inputCheckbox value="{!objectRow.selected}"/>
          </apex:column>
          <apex:column headerValue="Name" value="{!objectRow.name}" 
                                          width="90%"/>
        </apex:pageBlockTable>
      </apex:PageBlock>
    </apex:repeat>
    
    <apex:PageBlock >
      <apex:commandButton action="{!doClone}" value="Clone"/>
    </apex:PageBlock>
  </apex:form>  

</apex:page>
Custom controller
/**
* @description Helper class to clone parent and its children sObjects
*/
global without sharing class EnhancedCloneHelper {
       
    /**
    * @description Clone parent and its children
    * @param id of the parent sObject
    * @return String Serialized result
    */
    webservice static String clone (String sObjectId) {
        
        List<Sections__c> parentSObjects;
        Sections__c parent;
        CloneModel cloneModelResult = new CloneModel();
        
        // Parent query
        String query = String.format(
            'SELECT {0} FROM {1} WHERE Id = \'\'{2}\'\'',
            new String[] {
                String.join(
                    new List<String>(
                        Sections__c.SObjectType.getDescribe().fields.getMap().keySet()
                    ),
                    ','
                ),
                String.valueOf(Sections__c.SObjectType),
                sObjectId
           }
        );

        try {
            
            // Query and gets results
            parentSObjects = Database.query(query);         
            
            // Clone the original object. Here you can change anything without affecting the original sObject
            parent = parentSObjects[0].clone(false, true, false, false);
            //parent.Name = parent.Name + 'CLONED';

            Database.insert(parent);
       
        } catch (DmlException error) {
            cloneModelResult.message = 'An error occurred while cloning the object.' + error.getMessage();
            return JSON.serialize(cloneModelResult);        
        }
        
        // Children query  
        query = String.format(
            'SELECT {0} FROM {1} WHERE Sections__c = \'\'{2}\'\'',
            new String[] {
                String.join(
                    new List<String>(
                        Project_List__c.SObjectType.getDescribe().fields.getMap().keySet()
                    ),
                    ','
                ),
                String.valueOf(Project_List__c.SObjectType),
                sObjectId
           }
        );
        
        List<Project_List__c> children = new List<Project_List__c>();
        
        try {
            
            // Query and clone the children. Here you can change anything without affecting the original sObject
            for (Project_List__c child:(List<Project_List__c>)Database.query(query)) {
                children.add(child.clone(false,true,false,false));
            }
            
            // If there isn't any children ends the process and return success
            if (children.isEmpty()) {
                cloneModelResult.isSuccess = true;
                cloneModelResult.message = 'Object successfully cloned!';                
                cloneModelResult.url = getUrlRedirect(parent.Id);
                return JSON.serialize(cloneModelResult);
            }
            
            // Set the parent's Id
            for (Project_List__c child : children) {
                child.Additional_Items__c = parent.Id;
            }
        
            Database.insert(children);
    
            
        }  catch(DMLException error) {
            cloneModelResult.message = 'An error occurred while cloning the object.' + error.getMessage();
            return JSON.serialize(cloneModelResult); 
        }
        
        // Return success at the end of the process
        cloneModelResult.isSuccess = true;
        cloneModelResult.message = 'Object and its children successfully cloned!';
        cloneModelResult.url = getUrlRedirect(parent.Id);
        
        return JSON.serialize(cloneModelResult);

    }

    private static String getUrlRedirect(String sObjectId){
        PageReference page = new PageReference('/'+ sObjectId);
        return page.getUrl();
    }
    
    global class CloneModel {
        Boolean isSuccess;
        String message;
        String url;
    }
}
can anyone help me over here.

Thanks in advance.

regard,
naga