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
Nirmal9114Nirmal9114 

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>


 
thienthangthienthang

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.
Nirmal9114Nirmal9114
Hi ,

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
Sumit Kumar Singh 9Sumit Kumar Singh 9
You are calling a method which doesn't exists.
replace action with "cloneAcc"

<apex:page standardController="Account" extensions="AccountClone_Controller" action="{!cloneAcc}">
</apex:page>

Thanks,
Sumit Kumar Singh
 
Nirmal9114Nirmal9114
sumit - THANKS i did it. its working

but jus can you help me out how to populate the deailed of account from which i have cloned the new account
Sumit Kumar Singh 9Sumit Kumar Singh 9
I didn't get your question properly. Elaborate it in more detail. 
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
thienthangthienthang

Hi,
Execuse me for my poor explaination. Let's me go for detail as following

If you make a test with something like following, 

<apex:page standardController="Account" extensions="AccountClone_Controller" action="{!doSomething}"> 
</apex:page>
A warning message immediately appear on visualforce page while you try to save the page, because doSomething doesn't exist in controller.

But with the following code, we still can save the vf page without any warning message
<apex:page standardController="Account" extensions="AccountClone_Controller" action="{!clone}"> </apex:page>

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
<apex:page standardController="Account" extensions="AccountClone_Controller" action="{!cloneAcc}">
</apex:page>

After change the code to this and run, the another error happen at this line
insert newAccount;

because the lack of required value: Account Name. 

So, I did a small change to fix the issue like the following
Controller:
public 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() 
    {
        this.record.Name = 'Test'; //Just for testing only
        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:
<apex:page standardController="Account" extensions="AccountClone_Controller" action="{!cloneAcc}">
</apex:page>


 
Nirmal9114Nirmal9114
SUMIT - i have created the clone button already and set it to vf page
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
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Tanya, 

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}