You need to sign in to do that
Don't have an account?
Manish Anand 10
How to add a custom button created in VF, to the object page layout?
Hi,
I have created a custom button in VF, using commandButton.Clicking on this button should update a custom field.
Though, I have implemented this, I have couple of challenges.
1)How do I add this button on Object Page layout, such that it appears on the Object detail page?
2)Clicking on the button, updates the field.But the updated value is not shown, until the page is refresh
I want, updated value to be shown, when button is clicked and without refreshing the page.
Below is my controller extension class.
I have created a custom button in VF, using commandButton.Clicking on this button should update a custom field.
Though, I have implemented this, I have couple of challenges.
1)How do I add this button on Object Page layout, such that it appears on the Object detail page?
2)Clicking on the button, updates the field.But the updated value is not shown, until the page is refresh
I want, updated value to be shown, when button is clicked and without refreshing the page.
Below is my controller extension class.
public class ControllerEx3 { private final Account acct; public ControllerEx3(ApexPages.StandardController stdcontroller) { this.acct = (Account)stdcontroller.getRecord(); } Public pagereference Activate() { string stat; Account act; act=[Select name, status__c from Account where ID=:acct.ID]; stat=act.status__c; if (stat!='Active') { act.status__c='Active'; update act; } return new PageReference('/'+act.Id); } }
Don't try to save the code using your Developer Mode editor. Always use the regular VF page editor so that you will not get any issues.
Regards,
Mahesh
All Answers
1. In your visualforce page you will need to set the standard controller page attribute to the object name you want to add it to, and set your controllerEx3 class as the extension controller (I suggest renaming this class to something more relevant).
2. You can use the rerender attributre in your visualforce page to rerender an element on the page that is displaying the output value. After you have added the VF page to a page layout, if the field is also on the page layout, you may want to reload the page by setting the parent window url.
Hope that helps,
Richard.
Please find the modified Class:
Visualforce Page:
Create the button on Account object:
I also tested the code in my DE environment and it is working fine.
Please do let me know if it helps you.
Regards,
Mahesh
I see it's working.Thanks.
However, when I tried to save the code (either apex/vf) it gives run time exception.Please see the screen shot attached.
Also, I don't see any difference in yours extension class and mine. It throws the same exception for my extension class.
Can it be avoided?
Exception is-System.QueryException: List has no rows for assignment to SObject
Error is in expression '{!Activate}' in component <apex:page> in page testscenario9: Class.ControllerEx3.Activate: line 9, column 1
line 9 is SOQL query, select statement.
For something like this where the update is very simple and there is no complex business logic, another way to achieve this is to use a Javascript custom button.
Create a custom button with the following settings:
Behaviour: Execute Javascript
Content Source: OnClick Javascript
Then paste in the following javascript code: Add the button to the Account page layout.
Thanks,
Richard.
Don't try to save the code using your Developer Mode editor. Always use the regular VF page editor so that you will not get any issues.
Regards,
Mahesh