+ Start a Discussion
John Neilan 2John Neilan 2 

Clone Custom Object Record & Redirect to VF Page

Hello,

I have a button on the Opportunity object that clones a record from a related custom object.  The clone works fine, but it opens the standard edit page for the custom object.  Instead, I would like the clone to open a custom VF edit page.  Is this possible to do?  My current URL for the button is:
 
onClick="window.open('/{!Opportunity.Sumary__r.Id}/e?clone=1','_blank')"

 
Best Answer chosen by John Neilan 2
ClintLeeClintLee
You could approach it in the following way:

1. Create a Custom Controller for your Visualforce edit page.
public with sharing class Ctrl_SummaryClone
{
    public Summary__c summary { get; set; }

    public Ctrl_SummaryClone()
    {
        String summaryId = ApexPages.currentPage().getParameters().get('summary_id');
        
        // query all necessary fields.
        summary = [select Id, Name from Summary__c where Id = :summaryId];
    }

   /**
    *  Clone the Summary and save as a new record.  
    *  Return the user to the detail page of the new record.
    * 
   **/
    public PageReference cloneSummary()
    {
        Summary__c clonedSummary = summary.clone(false,true,false,false);
        insert clonedSummary;
        return new PageReference('/' + clonedSummary.Id);
    }
}
2. Create a Visualforce edit page named SummaryClone.  Now that you have the original Summary__c record stored in your controller you can edit the field values using apex:inputFields.
<apex:page controller="Ctrl_SummaryClone">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockButtons>
                <apex:commandButton value="Save" action="{!cloneSummary}" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Name" />
                    <apex:inputField value="{!summary.Name}" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

3. Create a new custom button on the Opportunity object.  The button should be a Detail Page button type and open a URL.  
The URL would be apex/SummaryClone?summary_id={!Opportunity.Summary__r.Id}

Hope that helps,

Clint
 

All Answers

ClintLeeClintLee
You could approach it in the following way:

1. Create a Custom Controller for your Visualforce edit page.
public with sharing class Ctrl_SummaryClone
{
    public Summary__c summary { get; set; }

    public Ctrl_SummaryClone()
    {
        String summaryId = ApexPages.currentPage().getParameters().get('summary_id');
        
        // query all necessary fields.
        summary = [select Id, Name from Summary__c where Id = :summaryId];
    }

   /**
    *  Clone the Summary and save as a new record.  
    *  Return the user to the detail page of the new record.
    * 
   **/
    public PageReference cloneSummary()
    {
        Summary__c clonedSummary = summary.clone(false,true,false,false);
        insert clonedSummary;
        return new PageReference('/' + clonedSummary.Id);
    }
}
2. Create a Visualforce edit page named SummaryClone.  Now that you have the original Summary__c record stored in your controller you can edit the field values using apex:inputFields.
<apex:page controller="Ctrl_SummaryClone">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockButtons>
                <apex:commandButton value="Save" action="{!cloneSummary}" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Name" />
                    <apex:inputField value="{!summary.Name}" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

3. Create a new custom button on the Opportunity object.  The button should be a Detail Page button type and open a URL.  
The URL would be apex/SummaryClone?summary_id={!Opportunity.Summary__r.Id}

Hope that helps,

Clint
 
This was selected as the best answer
John Neilan 2John Neilan 2
Clint,

Thanks!  That actually helps me with another issue I was trying to solve!  For this issue, all I had to do was replace the Clone button on my custom object with my VF "edit" page on that same object and it now clones my record but opens it in the custom Vzf page rather than the standard edit page.  Thanks for your help!