+ Start a Discussion
Frank JordanFrank Jordan 

Update a field with a visualforce button

I need a custom button  that when pressed, changes the owner of the record and saves the record. I have the class already written, I just need the apex page to go along with it. 

Here is the class that I have: 


public with sharing 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.Business_Unit__c = 'EIS Queue';
         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);
      }
    }

}
Best Answer chosen by Frank Jordan
geeta garggeeta garg
Hi Frank,

You can try this code.
<apex:page extensions="classSubmitToLegal" standardController="Agreement_Request__c ">
<apex:form>
<apex:commandButton action="{!someMethod}" value="Submit To Legal" id="theButton"/>
</apex:form>
</apex:page>

All Answers

KevinPKevinP
Frank,

You'll want to research more into how action buttons function, and to avoid just doing your homework for you, i've intentionally not wired this basic visualforce page up to your button properly. Regardless, this should help you out:
 
<apex:page controller="classSubmitToLegal">
<apex:form>
<apex:commandButton action="{!save}" value="Submit To Legal" id="theButton"/>
</apex:form
</apex:page>

 
geeta garggeeta garg
Hi Frank,

You can try this code.
<apex:page extensions="classSubmitToLegal" standardController="Agreement_Request__c ">
<apex:form>
<apex:commandButton action="{!someMethod}" value="Submit To Legal" id="theButton"/>
</apex:form>
</apex:page>
This was selected as the best answer