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
Frank JordanFrank Jordan 

How to link Visualforce button to custom detail page button

I have the following class and visualforce page created, so when the button is pressed, the record owner is updated. I would like to add this button to the detail page like the button below. Can anyone help? 

User-added image

Class: 
public class classSubmitToLegal {

    public Agreement_Request__c a{get;set;}
    public classSubmitToLegal() {
     a = new Agreement_Request__c ();
     a = [select Business_Unit__c from Agreement_Request__c Where Id=: ApexPages.currentPage().getParameters().get('Id')];
    }
    
    public void someMethod() {
      if(a.Business_Unit__c == 'EIS') {
         a.OwnerID ='00G18000000ZRlS';
         update a;
          Apexpages.Message errorMessage = new Apexpages.Message(ApexPages.Severity.Info,'You have changed the owner');
          Apexpages.addMessage(errorMessage);
      } else {
          Apexpages.Message errorMessage = new Apexpages.Message(ApexPages.Severity.Info,'You cannot update');
          Apexpages.addMessage(errorMessage);
      }
    }

}

VF Page:
 
<apex:page controller="classSubmitToLegal"> 
<apex:form>
<apex:commandButton action="{!someMethod}" value="Submit To Legal" id="Submit_to_Legal__c"/>
</apex:form>
</apex:page>

 
Best Answer chosen by Frank Jordan
Swayam@SalesforceGuySwayam@SalesforceGuy
Hi 

You can do it via two ways :

1.  Creating a custom button and calling Visualforce page. Please go here (https://developer.salesforce.com/docs/atlas.en-us.workbook_vf.meta/workbook_vf/overrides_3.htm)

2.
  •  Update your visual force this line 
​<apex:page standardController="Name Of Object"  extension="classSubmitToLegal">
  • Update your controller 
    public class classSubmitToLegal {
    
        public Agreement_Request__c a{get;set;}
        public classSubmitToLegal(ApexPages.StandardController stdController) {
    	
    	  this.a = (Agreement_Request__c)stdController.getRecord();
        }
        
        public void someMethod() {
          if(a.Business_Unit__c == 'EIS') {
             a.OwnerID ='00G18000000ZRlS';
             update a;
              Apexpages.Message errorMessage = new Apexpages.Message(ApexPages.Severity.Info,'You have changed the owner');
              Apexpages.addMessage(errorMessage);
          } else {
              Apexpages.Message errorMessage = new Apexpages.Message(ApexPages.Severity.Info,'You cannot update');
              Apexpages.addMessage(errorMessage);
          }
        }
    
    }

     
  • Edit detail page layou and add Inline visual force page to wherever you want.


Hope this helps,

--
Thanks,

Swayam