I've got an apex class with a webservice method that I'm trying to fire from a custom button on the page layout of the object the method will be doing stuff to.

My javascript for the custom button that fires the Apex webservice is pretty basic:


var myvar = sforce.apex.execute("ActOn_Scheduled", "UpdateMsgStats", {msg: {!THIS.SOBJECTRECORD}});

As the code above shows, one of the parameters to my "UpdateMsgStats" method is 'msg', which is the actual record for the custom sObject "Message". I've stuck in the fake merge field "{!THIS.SOBJECTRECORD}" as a placeholder to give you all an idea of what I'm looking for.

I suppose I could pass the record's ID instead, and use a SOQL query to get the actual sObject record back, but I'm wondering if there's a way to pass the entire object using a merge field, or something similar?

edit: is the proper merge field by chance "{!$ObjectType.mycustomobject__c}"? Never used that one before...

Another related question: there's actually a second parameter to this method, the oAuth access token in the form of a string. I have a separate method in the class that makes an API callout to get the string and stores it in a variable in the actual class. Is there a way to pull a variable from the referenced class and pass it via the javascript button? Or should I just call the authorization method within the webservice?

1 Answer 1




in your apex, unless you know this will always be a specific type, you'll have to do some gymnastics to get the type. Something like this:


which will return a "token" that you can cast to a string for dynamic soql and construct a query to get the record by id:

String sobj = (String) id.getSObjectType();
String soqlString = 'select whatever, from ' + sobj + ' where id = \'' + id + '\''
  • Kevin, thanks for your answer, but I'm not clear: {!ID} in my apex? Where in my apex would this play a role? I'm looking for the merge field to pass the current object record my javascript button was clicked on TO my apex method.
    – smohyee
    Jun 13, 2014 at 21:31
  • 1
    No - {!id} as the merge field in the Javascript; Let the Apex figure out which sObject it is - unless you know that the webservice only handles a single sobject (Message__c). Then apex queries for the fields on that sobject using the id in the where clause
    – cropredy
    Jun 14, 2014 at 0:54
  • 1
    @smohyee, as crop1645 said, use {!Id} in your js button definition. Something like: var myvar = sforce.apex.execute("ActOn_Scheduled", "UpdateMsgStats", {msg: {!Id}});
    – Kevin P
    Jun 14, 2014 at 14:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .