0

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();"/>
    &nbsp;&nbsp;&nbsp;

    <apex:commandButton Value="Approve - Send to VP" onClick="vp();"/>

</div>
</apex:form>
</apex:page>
0

You can take define your custom controller and add your logic in the Apex class. Take help from following links on how to create a custom controller and how to define approval process logic in an Apex class: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_def.htm https://trailhead.salesforce.com/en/content/learn/modules/visualforce_fundamentals/visualforce_custom_controllers https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_process_example.htm

  • Thanks. I know how to enter an approval process from the beginning in a controller. I don't know how to enter one that is already in progress. – J. Neilan Apr 9 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.