You need to sign in to do that
Don't have an account?
Nirmal9114
Clone functionality on Account object with related list
I am having this error "Return type of an Apex action method must be a PageReference. Found: core.apexpages.el.adapters.ApexObjectValueELAdapter "
PLEASE HELP
my controller code is:
global with sharing class AccountClone_Controller {
private final Id recordId;
private final Account record;
private final ApexPages.StandardController controller;
public AccountClone_Controller(ApexPages.StandardController controller) {
this.controller = controller;
this.record = (Account) (this.controller.getRecord());
this.recordId = this.controller.getId();
}
public PageReference cloneAcc()
{
Account newAccount = this.record.clone(false, true, true, false);
insert newAccount;
// cloning related list contacts
List<Contact> clonedContacts = new List<Contact>();
for(Contact contact : [SELECT Id, LastName, AccountId FROM Contact WHERE AccountId = : this.recordId])
{
Contact clonedContact = Contact.clone(false, true, true, false);
clonedContact.AccountId = newAccount.Id;
clonedContacts.add(clonedContact);
}
insert clonedContacts;
return new ApexPages.StandardController(newAccount).view();
}
}
VF PAGE :
<apex:page standardController="Account" extensions="AccountClone_Controller" action="{!clone}">
</apex:page>
PLEASE HELP
my controller code is:
global with sharing class AccountClone_Controller {
private final Id recordId;
private final Account record;
private final ApexPages.StandardController controller;
public AccountClone_Controller(ApexPages.StandardController controller) {
this.controller = controller;
this.record = (Account) (this.controller.getRecord());
this.recordId = this.controller.getId();
}
public PageReference cloneAcc()
{
Account newAccount = this.record.clone(false, true, true, false);
insert newAccount;
// cloning related list contacts
List<Contact> clonedContacts = new List<Contact>();
for(Contact contact : [SELECT Id, LastName, AccountId FROM Contact WHERE AccountId = : this.recordId])
{
Contact clonedContact = Contact.clone(false, true, true, false);
clonedContact.AccountId = newAccount.Id;
clonedContacts.add(clonedContact);
}
insert clonedContacts;
return new ApexPages.StandardController(newAccount).view();
}
}
VF PAGE :
<apex:page standardController="Account" extensions="AccountClone_Controller" action="{!clone}">
</apex:page>
Hi,
(I am sorry for my bad english).
The error happen because when the vf page is loaded, it call a standardcontroller function (clone) but this function not return the PageReference. Second, the name of action and method in the controller are not the same: action="{!clone}" should be action="{!cloneAcc}"
And you forget to add the name for account before insert (this will take you to another error also). Try to put this
this.record.Name = 'Test'; //Or any name you like
before this line:
Account newAccount = this.record.clone(false, true, true, false);
to make sure the record have required value.
Hope this help.
I am SORRY i am not able to get my mistake when you say "vf page is loaded, it call a standardcontroller function (clone) but this function not return the PageReference."
Can you jus pin point my mistake.
Thanks
replace action with "cloneAcc"
<apex:page standardController="Account" extensions="AccountClone_Controller" action="{!cloneAcc}">
</apex:page>
Thanks,
Sumit Kumar Singh
but jus can you help me out how to populate the deailed of account from which i have cloned the new account
If you want to have a button on Account Detail page to clone the Account with their contacts, then you can do the following -
1) You can create a "Custom Detail Button" on the account with URL type -
/apex/visualforccePageName?id={!Account.Id}
OR
2) You can directly choose "Visualforce page".
Thanks,
Sumit Kumar Singh
Hi,
A warning message immediately appear on visualforce page while you try to save the page, because doSomething doesn't exist in controller.Execuse me for my poor explaination. Let's me go for detail as following
If you make a test with something like following,
But with the following code, we still can save the vf page without any warning message
And In my opinion, error happen when the page is loaded (requested by the server) because clone does exist as an method of standardcontroller but return different type. (Not PageReference)
Because if this method return null, the page just refresh without any error.
And because in your controller defined method name cloneAcc. Then the vf page should be like @Sumit Kumar Singh 9 mentioned
After change the code to this and run, the another error happen at this line
because the lack of required value: Account Name.
So, I did a small change to fix the issue like the following
Controller: VF:
But my need is when i click that clone button on account FIRST it should open in edit mode and populate all the fields from the account record from which i m cloning.
SECOND - user has to select whether to save it or not.(if it opens in edit mode save cancel button will come i guess)
Right now its saving automatically. PLEASE HELP
I can redirect to standard EDIT page. But how would you clone the records as "There is no way to add a buuton on Satandard Edit page."
Edit the button -
Behavior - Display in existing window without sidebar or header.
Content source - URL
/{!Account.Id}/e?retURL=%2F{!Account.Id}