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
Richa_LearningRicha_Learning 

Urgent Please help - Custom button cloning issue

All,

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){
                setExFields.add(excludedField.Name.toLowerCase());
            }
          
            for(String s: mapAccountFields.keyset()){
                if(!setExFields.contains(s)){
                    if(queryString == ''){
                        queryString += s;
                    }else{
                        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('/'+objaccount.id+'/e?clone=1');
        cloneaccount.setRedirect(true);
        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
http://raydehler.com/cloud/clod/salesforce-url-hacking-to-prepopulate-fields-on-a-standard-page-layout.html
Richa_LearningRicha_Learning
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 
'001/e?FieldId=Value1&FieldId2=Value2

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
http://andyinthecloud.com/2014/01/05/querying-custom-object-and-field-ids-via-tooling-api/

Another hack to find field Ids. although seems like its broken now
https://force201.wordpress.com/2014/02/27/finding-visualforce-field-ids/

Information about fieldset 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_fields_describe.htm
 
Richa_LearningRicha_Learning
Thanks RZ. I am not using class and VF page now . I used this link as mentioned by you to populate the fields:
http://raydehler.com/cloud/clod/salesforce-url-hacking-to-prepopulate-fields-on-a-standard-page-layout.html

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?
 
/001/e?retURL=/{!Account.Id}

&CF00N63000000HV70={!Account.Description__c}

 
R Z KhanR Z Khan
Hi,

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
Richa_LearningRicha_Learning
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]}"/>
              </apex:repeat> 

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