You need to sign in to do that
Don't have an account?

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:
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; } }
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
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
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?
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