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

Urgent Please help - Custom button cloning issue


I have logic on my controller that it should clone few fields only.....I want to clone only those fields that are not in cloneaccount custom settings...below controller clones everything .............

Also, we have workflow rules that sends out an email as soon as record is created..i want email to only go out when i hit save.....not when i hit clone.....

Please let me know what's wrong with the below controller:
public with sharing class AccountController {

    public Account objAccount {get;set;}        
    public string AccountID;                        
      set<String> setExFields = new set<String>();
    private Account objNewAccount;
     Map<String, Schema.SObjectField> mapAccountFields;
    public AccountController(ApexPages.StandardController controller) 
              public PageReference autoRun()
        AccountID = ApexPages.currentPage().getParameters().get('id');
        if(AccountID != null)

        {        mapAccountFields = Schema.SObjectType.Account.fields.getMap() ;
		List<CloneAccount__c > Exfields = Cloneaccount__c.getall().values();
            for( Cloneaccount__c excludedField: ExFields){
            for(String s: mapAccountFields.keyset()){
                    if(queryString == ''){
                        queryString += s;
                        queryString += ',' + s;

                objnewaccount = Database.Query('Select ' + queryString + ' From account where id= \'' + String.escapeSingleQuotes(accountID) + '\'');   

                objAccount = objNewAccount.clone(false,true,false,false);

        //     insert objaccount;

       PageReference cloneaccount = new PageReference('/''/e?clone=1');
        return accountPage;
return null;


R Z KhanR Z Khan
1. issue is that if you dont isnert your account the Id is null so it cant send u anywhere.
2. clone will clone() makes the exact copy of the record so it copies all fields. if you onyl want certain fields create a new Account instance and then iterate over your fieldset and assign values from your original account.
3. In order to redirect to an edit page of account you should return new PageReference('001/e')
4. In order to prepopulate the fields, pass them as parameters in your url. For that you would need to use field Id for the fields you want to copy. More about this in the article below
thanks R Z..i am fairly new to Apex...i woul appreciate if you could help to fix the controller.....In my controller i have aded the logic to clone only few fields.....
R Z KhanR Z Khan
For your problem you don tneed to call clone at all. 
Instead of using customs ettings for a lsit of fields try using fieldset. 
All you need is to find field Ids. There is no easy native way to do it. Some solutions are using tooling api. You could also use you field csutom setting that will store field api name and field id. You can access field Id byt clicking on a field name when u click on Account object page. Field will be a part of URL.  

If you decide to go with cusotm setting to store your fields ids, then u dont need to worry about fieldset. 

Once you populated field ids then redirect to 

where field Id is the id of your field and value is the prepopulated value you want your new record to have

Information hwo to retrieve Field id using tooling API

Another hack to find field Ids. although seems like its broken now

Information about fieldset
Thanks RZ. I am not using class and VF page now . I used this link as mentioned by you to populate the fields:

Only issue is I am not able to populate the text area field. I can see value is getting copied on the url but it is not showing on the field. Is there a workaround?


R Z KhanR Z Khan

1 i wouldnt put a long description field in the URL, its not a best practice. Why don't u replace the standard edit page witha Visualforce page? Seems like that will give you all the flexibility you need
I only need to copy 2 fields so i thought to use url hack. Also, I am not much familiar with vf page. If you have some sample please share.
R Z KhanR Z Khan
<apex:page standardController="Account" extension="AccountController">
 <apex:repeat value="{!$ObjectType.Account.FieldSets.YOUR_FIELDSET_NAME}" var="f">
                  <apex:inputField value="{!account[f]}"/>

    <br /><apex:commandButton value="Submit" action="{!save}" />