0

I have made a custom button that launches an apex class from the following javascript code:

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

{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}

var contextUser = sforce.apex.execute("QRSBridge", "generateQRS", {});

window.alert('A QRS has been generated.');

This is a custom button on a record, not in a visual force page, that is in an object with multiple other records, possibly with the same name.

I am interested in only querying this record (one that had the button pressed) from my apex class that is executed. Is there any way for me to find the record ID or to limit the query to only this record, the one that had the button pressed? I'm assuming I could pass a variable through javascript?

Any help is appreciated.

1 Answer 1

0

If you add an ID parameter to your Apex method:

global with sharing class QRSBridge {
    WebService static String generateQRS(Id myId) {
        // Query using the ID
        ....
    }
}

then you can pass the ID from your record detail page (assuming the object name is My__c) like this:

var result = sforce.apex.execute("QRSBridge", "generateQRS", {myId: '{!My__c.Id}'});

Probably worth changing the version number in your REQUIRESCRIPT to something newer e.g. 35.0 too.

3
  • I'm trying to basically query just one record, the record that the button was pressed on. I feel as though the object name is slightly irrelevant. Currently I'm hunting down what to do if I can get the record ID, can I query solely on the record ID? I'm trying to query an object solely from one record within an object, if that makes sense.
    – JMK
    Jan 5, 2016 at 22:48
  • @JMK The detail page uses a standard controller that automatically makes the symbol My__c.Id available to the custom button with the ID of the object being shown in the detail page assuming the object name is My__c (substitute your object type).
    – Keith C
    Jan 5, 2016 at 22:52
  • Here is an example for a standard object How to get Case Id of current case when button is clicked on case page?.
    – Keith C
    Jan 5, 2016 at 22:55

You must log in to answer this question.

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