+ Start a Discussion
Parvinder ChanaParvinder Chana 

Show custom button (execute java script) on visual force page

Hello all,

I created a custom button on Case object and purpose of the button is to escalate the case. Here is the code and it works great.

{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")} 

var caseObj = new sforce.SObject("Case"); 
caseObj.id = '{!Case.Id}'; /* Need Id field to update Case */ 

if ({!Case.IsEscalated} == 0) 

caseObj.IsEscalated= 1; 
}; 


/* Change escalated field */ 

/* update method takes an array of Cases; init to 1 element - 'caseObj' */ 
var result = sforce.connection.update([caseObj]); 

if (result[0].success == 'false') { 
alert(result[0].errors.message); 

}

Now I want to use this custom button in visual force page and add that VF page on feed view, this is what I did with VF page:

<apex:page standardController="Case" extensions="CaseController" tabStyle="Case" >
  <apex:form >
  <apex:commandButton action="{!URLFOR($Action.Case.Escalate, Case.Id)}" value="Escalate"/>
  </apex:form>
</apex:page>

I added above VF page on feed view layout and it shows up the custom button as seen below

Escalate Button

but when I click the button, it doesn't execute the java scripts and shows the following windows after button click

Escalate After Click

Thanks in advance for help!

P
Best Answer chosen by Parvinder Chana
deepak balur 19deepak balur 19
Can you try this please:
public with sharing class Parvinder_CaseController {

 public ApexPages.StandardController stdController;
       
  public Parvinder_CaseController(ApexPages.StandardController Controller) {
     stdController    =Controller;
    }

   public void  Escalate(){
        Case C = (Case)stdController.getRecord();
        Case CUpd = new Case();
         CUpd.Id= C.Id;
         CUpd.Status='High';
        update CUpd;
    } 
}

All Answers

deepak balur 19deepak balur 19
Your action has to be a method which you have to define in your controller class. In that Controller class you will set the value of the escalation field. For buttons on VF page that's the way to go. For the code that you have written, it works perfectly fine on a standard page.
Parvinder ChanaParvinder Chana
Hi Deepak, Appreciate the quick reply but unfortnately I'm not a developer, if you can provide some more details on how to add this to controller class that will be very helpful. Thanks!
deepak balur 19deepak balur 19
Can you try this please:
public with sharing class Parvinder_CaseController {

 public ApexPages.StandardController stdController;
       
  public Parvinder_CaseController(ApexPages.StandardController Controller) {
     stdController    =Controller;
    }

   public void  Escalate(){
        Case C = (Case)stdController.getRecord();
        Case CUpd = new Case();
         CUpd.Id= C.Id;
         CUpd.Status='High';
        update CUpd;
    } 
}
This was selected as the best answer
Parvinder ChanaParvinder Chana
Thanks for your help Deepak. It helped me to develop the solution. Only challenge I ran into was to refresh the tab in console for which I found help on internet. Appreciate your help with this.

Cheers!
P