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
Manish Anand 10Manish 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.
  
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);
     
   }
 }

 
Best Answer chosen by Manish Anand 10
Mahesh DMahesh D
Hi Manish,

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

Richard Jimenez 9Richard Jimenez 9
Hi,

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.
Mahesh DMahesh D
Hi Manish,

Please find the modified Class:
 
public class ControllerEx3 {
    private final Account acct;
    public ControllerEx3(ApexPages.StandardController stdcontroller) {
        this.acct = (Account)stdcontroller.getRecord();
    }
   
    Public PageReference Activate() {
        
        Account act = [Select Id, Name, Status__c from Account where ID=:acct.ID];
        if(act.Status__c != 'Active') {
            act.status__c='Active';
            update act;
        }
        return new PageReference('/'+act.Id);
    }
}

Visualforce Page:
 
<apex:page standardController="Account" extensions="ControllerEx3" action="{!Activate}">

</apex:page>
Create the button on Account object:

User-added image

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

 
Manish Anand 10Manish Anand 10
Hi 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?
User-added image

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.
 
Richard Jimenez 9Richard Jimenez 9
Hi Mahesh,

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:
Name: Activate Account
Display Type: Detail Page Button
Behaviour: Execute Javascript
Content Source: OnClick Javascript

Then paste in the following javascript code:
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}

if('{!Account.Status__c}' != 'Active')
{
   var accountObj = new sforce.SObject("Account");
   accountObj.Id = '{!Account.Id}'; 
   accountObj.Status__c = 'Active';
   sforce.connection.update([accountObj]);

   window.location.reload();
}
Add the button to the Account page layout.

Thanks,
Richard.
 
Mahesh DMahesh D
Hi Manish,

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
This was selected as the best answer
Manish Anand 10Manish Anand 10
Thanks All