I have a Visualforce page that is designed to allow an approver to either approve/reject an Opportunity as a Final decision, or approve the Opportunity and then send it on for another approval. The VF page provides 2 buttons to allow the user to do this and the buttons call in Javascript functions. I would like to convert those Javascript functions into a controller to use with the VF page as it would allow me to do a few other things, but I'm not quite sure how to do so. The code I have so far is below:
Existing VF Page:
<apex:page standardController="Opportunity" >
<script src="/soap/ajax/26.0/connection.js"/>
<script src="/soap/ajax/26.0/apex.js"/>
<script src="/support/console/27.0/integration.js"/>
<script type="text/javascript">
sforce.connection.sessionId = "{!GETSESSIONID()}";
function vp()
{
var opp = new sforce.SObject("Opportunity");
opp.id = "{!Opportunity.Id}";
var result = sforce.connection.query("SELECT Id FROM ProcessInstanceWorkitem WHERE processInstance.TargetObjectId='{!Opportunity.Id}'");
var approvals = result.getArray("records");
opp.AppRejStat__c = "{!$Label.App_VP}";
var approvalID = approvals[0].Id;
window.open('/p/process/ProcessInstanceWorkitemWizardStageManager?id='+approvalID);
sforce.connection.update([opp]);
return this;
};
function final()
{
var opp = new sforce.SObject("Opportunity");
opp.id = "{!Opportunity.Id}";
var result = sforce.connection.query("SELECT Id FROM ProcessInstanceWorkitem WHERE processInstance.TargetObjectId='{!Opportunity.Id}'");
var approvals = result.getArray("records");
opp.AppRejStat__c = "{!$Label.App_Final}";
var approvalID = approvals[0].Id;
window.open('/p/process/ProcessInstanceWorkitemWizardStageManager?id='+approvalID);
sforce.connection.update([opp]);
return this;
};
<apex:form >
<div style="text-align: left"><p/>
<apex:commandButton Value="Approve/Reject - Final" onClick="final();"/>
<apex:commandButton Value="Approve - Send to VP" onClick="vp();"/>
</div>
</apex:form>
</apex:page>