You need to sign in to do that
Don't have an account?
Raghu Sharma 6
Cloning a record
I want to add a button to clone the existing record. What is the easy and best approach to add code to my below code to make it work? Do we need controller extension to achieve this?
<apex:page StandardController="Account"> hello {!$User.LastName} <apex:form > <apex:pageBlock > <apex:pageBlockSection > <apex:inputfield value="{!account.name}"/> <apex:inputfield value="{!account.AccountNumber}"/> </apex:pageBlockSection> <apex:pageblockButtons > <apex:commandButton rendered="{!IF($CurrentPage.parameters.id !=null,true,false)}" action="{!save}" value="Save Account"/> <apex:commandButton rendered="{!IF($CurrentPage.parameters.id ==null,true,false)}" action="{!save}" value="Save Account"/> </apex:pageblockButtons> </apex:pageBlock> </apex:form> </apex:page>
You could use this code, I think this will resolve your issue.
If you want to complete the clone action by button click the you would requrie a custom apex controller for that.
Read This for more: http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/
Thanks
Shashikant
public class CloneAccountController {
//Dave Roberts
//based on Jeff Douglas' code at
//http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/
//launch with /apex/CloneAccount?id=xxxxxxxxxxxxxxx
//added an instance variable for the standard controller
private ApexPages.StandardController controller {get; set;}
// add the instance for the variables being passed by id on the url
private Account accnt {get;set;}
// set the id of the record that is created -- ONLY USED BY THE TEST CLASS
public ID newRecordId {get;set;}
// initialize the controller
public CloneAccountController(ApexPages.StandardController controller) {
//initialize the standard controller
this.controller = controller;
// load the current record
accnt = (Account)controller.getRecord();
}
// method called from the VF's action attribute to clone the accnt
public PageReference cloneAccount() {
// setup the save point for rollback
Savepoint sp = Database.setSavepoint();
Account newaccnt;
try {
//copy the account - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
accnt = [select Id, Name, description, type, phone, BillingAddress, Industry, ParentId, Websitefrom Account where id = :accnt.id];
newaccnt = accnt.clone(false);
newaccnt.Name = 'Cloned '+accnt.name;
insert newaccnt;
// set the id of the new accnt created for testing
newRecordId = newaccnt.id;
} catch (Exception e){
// roll everything back in case of error
Database.rollback(sp);
ApexPages.addMessages(e);
return null;
}
//what if they cancel?
//how to test Exception?
return new PageReference('/'+newaccnt.id+'/e?retURL=%2F'+newaccnt.id);
}
}//CloneAccountController
--- --- ---
<apex:page standardController="Account"
extensions="CloneAccountController">
<!-- action="{!cloneAccount}">-->
<!--changed so that force interim page allowing cancel -->
<apex:form >
<apex:inputField value="{!account.name}"/> <p/>
<apex:commandButton value="Clone" action="{!cloneAccount}"/>
</apex:form>
<apex:pageMessages />
</apex:page>
--- --- ---
@isTest
private class TestCloneAccountController {
//based on Jeff Douglas' code at
//http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/
static testMethod void testCloneAccntController() {
// setup a reference to the page the controller is expecting with the parameters
PageReference pref = Page.CloneAccount;
Test.setCurrentPage(pref);
// setup a ship to account
//or find a record
Account shipTo = new Account();
shipTo.Name = 'MYTESTACCNT';
shipTo.Type = 'Supplier';
insert shipTo;
// Construct the standard controller
ApexPages.StandardController con = new ApexPages.StandardController(shipTo);
// create the controller
CloneAccountController ext = new CloneAccountController(con);
// Switch to test context
Test.startTest();
// call the clone method
PageReference ref = ext.cloneAccount();
// create the matching page reference
PageReference redir = new PageReference('/'+ext.newRecordId+'/e?retURL=%2F'+ext.newRecordId);
// make sure the user is sent to the correct url
System.assertEquals(ref.getUrl(),redir.getUrl());
// check that the new account was created successfully
Account newAccnt = [select id from Account where id = :ext.newRecordId];
System.assertNotEquals(newAccnt, null);
// Switch back to runtime context
Test.stopTest();
}
}//TestCloneAccountController