function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
chantspelchantspel 

Call controller function from custom button

Hi,

I'm trying to add a custom button to the standard button panel on a custom object. This is the view brought up by the <apex:detail> tag. When I select a custom button, it gives me several options including Execute Javascript, url, visualforce page. How can I use it to call a function for THIS visual force page? Any ideas?

Best Answer chosen by Admin (Salesforce Developers) 
chantspelchantspel

Figure this out, this is the bit I needed to fix the issue:

 

 

var myId = "{!CustomObject__c.Id}";
//get the session ID - was missing this line
sforce.connection.sessionId = '{!$Api.Session_ID}';
sforce.apex.execute("CustomObjectController","myFunction", {id:myId}); 
window.location.reload();

 

 

All Answers

kritinkritin

In javascript you can call apex class methods

sforce.apex.execute("ClassName","Methodname",{parameter1:"{!Account.Type__c}",Parameter2:"{!Account.Sub_Type__c}", parameter3:"{!Account.Id}"}); window.location.reload();

chantspelchantspel

OK thanks, I did this and now I'm getting this error:

faultcode:'sf:INVALID_SESSION_ID', faultstring:'INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session'

 

My method takes no arguments and returns void. Is there something I need to pass along to get a session ID?

chantspelchantspel

Figure this out, this is the bit I needed to fix the issue:

 

 

var myId = "{!CustomObject__c.Id}";
//get the session ID - was missing this line
sforce.connection.sessionId = '{!$Api.Session_ID}';
sforce.apex.execute("CustomObjectController","myFunction", {id:myId}); 
window.location.reload();

 

 

This was selected as the best answer