1

I am using an embedded Visualforce page as a custom button to update a custom field on my Opportunity record. The page saves fine and shows on my page layout, but when I click it, nothing happens. Does anyone see anything that could be causing this in my code below?

VF Page:

<apex:page standardController="Opportunity" >

<apex:includeScript value="/soap/ajax/26.0/connection.js"/>

<script type="text/javascript">
    function evp();
    {
        var opp = new sforce.SObject("Opportunity");
        opp.id = //set Record Id here, potentially get it from the URL ?id= param

        opp.Approval_Status__c = 'Sent to EVP';
        sforce.connection.update([opp]); 
        window.location.reload(); //to reload the window and show the updated values
    }
</script>

<apex:form>
<div style="text-align: center"><p/>

    <apex:commandButton Value="EVP Approval" onClick="evp();return false;"/>

</div>

</apex:form>

</apex:page>
0

There are three mistakes in your embedded visual force page

  1. function evp(); remove semicolon from here.

  2. You didn't assign opportunity Id to opp.id.

  3. window.location.reload(); will not work here.

So I have fixed all these issues in new code :

<apex:page standardController="Opportunity" >

  <apex:includeScript value="/soap/ajax/26.0/connection.js"/>

  <script type="text/javascript">
     function evp()
     {
       var opp = new sforce.SObject("Opportunity");
       opp.id = "{!Opportunity.Id}";
       opp.Approval_Status__c = 'Sent to EVP';
       sforce.connection.update([opp]); 
       window.parent.location = '/' + opp.id;
     }
  </script>

  <apex:form >
     <div style="text-align: center"><p/>

         <apex:commandButton Value="EVP Approval" onClick="evp();return false;"/>

    </div>

  </apex:form>

</apex:page>
  • Thanks so much, the semi-colon was the main culprit! – J. Neilan Oct 18 '18 at 13:48
2

You apparently did not set the record ID.

opp.id = "{!Opportunity.Id}";

Every update requires either an External ID or a Salesforce ID. Also, you should probably handle the return value from sforce.connection.update to make sure no errors occurred; this would have let you know an error had happened.

try {
    sforce.connection.update([opp]);
catch(e) {
    alert("Error: "+e);
}
  • Thanks. I must have deleted the record ID assignment by mistake. However, it's still not doing anything and no errors are showing. – J. Neilan Oct 13 '18 at 1:07

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.