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

VF page error please help
Hi All,
I am trying to add a custom clone buon on Account. Here is my page:
Here is my controller:
When i click on clone buton on account i get the VF page error:
Could you please help. I am not sure what is wrong in the code.
Thanks
I am trying to add a custom clone buon on Account. Here is my page:
<apex:page standardController="Account" extensions="AccountController" action="{!autoRun}"> <apex:sectionHeader title="Auto-Running Apex Code"/> <apex:outputPanel > You tried calling Apex Code from a button. If you see this page, something went wrong. You should have been redirected back to the record you clicked the button from. </apex:outputPanel> </apex:page>
Here is my controller:
public with sharing class AccountController { public Account objAccount {get;set;} public string AccountID; 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() ; objNewAccount = [ select id,OwnerId,AccountId,Status__c from Account where id = :AccountID ] ; objAccount = objNewAccount.Account(false,true,false,false); } return null; } public PageReference save() { insert objAccount; return new PageReference('/'+objAccount.id); } }
When i click on clone buton on account i get the VF page error:
You tried calling Apex Code from a button. If you see this page, something went wrong. You should have been redirected back to the record you clicked the button from.
Could you please help. I am not sure what is wrong in the code.
Thanks
Just following up on my previous comment. Please use below mentioned autorun function definition for accomplish your intended functionality:
All Answers
Instead of inserting the account record in separate save method you should insert the account in autorun method itself and set the return type as return new pagereference('/'+objAccount.Id);
You visualfroce doesnt havy any logic in it. It only display that error message without any conditions. WHat is supposed to be displayed?
Just following up on my previous comment. Please use below mentioned autorun function definition for accomplish your intended functionality:
The following code should help
and VFpage would be
@R Z Khan Button was on the page layout. I was callin gthe code using custom button