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
BCDBSABCDBSA 

Custom clone button in VF to clone child record with parent.Name populated

Hi All,

I have two custom objects with Master-Detail relationships, A__c (parent) and B__c (child).

When clicking "New" button in related list of B__c in A__c page layout, a VF page, named newBrecord is generated to let user create a new B__c record. One of the fields is populated with A__c.Name. For example, in the VF newBrecord page, 

A__c.Name:                A0001       (auto populated parent name not editable field)
B__c.Date__c:            5/15/2015  (input field)
B__c.Description__c:  Asking Apex Questions (input field)
 
After clicking "Clone" button, the current B__c record should be saved, and a new VF newBrecord page should be in edit-mode with A__c.Name (not editable), Date__c, Description__c fields populated. The StandardController of the VF newBrecord page is B__c, and an extension is created to write APEX codes.

I am new to Salesforce and have worked on this functionality for a long time. I would really appreciate your help on this functionaly.
KaranrajKaranraj
Hi Jolie, By adding controller extension to your visualforce page you can able to do. Try the below code
 
public with sharing class CustomExtensionController {
    
    private ApexPages.StandardController sController;
    public CustomExtensionController (ApexPages.StandardController controller) {
        sController = controller;
    }
   
    public PageReference saveAndNew() {       
        try {
            // Save the current sObject
            sController.save();
            PageReference pr = new PageReference('/YourVisualforcePage'); //Enter your visualforcepage       
            pr.setRedirect(true);
            return pr;
        } catch(Exception e) {
            ApexPages.addMessages(e);
            return null;
        }
    }
}

Thanks,
Karanraj
 
BCDBSABCDBSA
Hi Karanraj,

Thank you for your quick response.
However, what I want is clone function. I already have the "Save and New" method in my extension. 

Thanks,