How can I get the ID of the record regardless of the type of tab that is displaying a custom button. For example at the minute I have a button on an Order object that gets the ID like so:


var sfdcUrlName = window.location.protocol + '//' + window.location.host;

var myParentTabId = '';

// Here evaluating a parent tab ID
var readParentId = function(result){
myParentTabId = result.id;


var objID = '';
var showObjectId = function(result) {
objID = result.id;

var openSubtab = function(result){
sforce.console.openSubtab(myParentTabId, sfdcUrlName + '/apex/VFPAGE?id=' + objID , true, 'VFPAGE',null);

As my visualforce page uses the Order Standard Controller (and I'd rather not change this) you may realise that if the primary Tab is not showing an Order detail ID then a visualforce error is displayed

Id XXXXXXXXXX is not valid for the Order standard controller

I know there are methods like getEnclosingTabID, getfocusedSubTab and getFocusedPrimaryTabObjectID as above and presumably I need to use a few in conjunction but whilst getEnclosingTabID gives me the focused tab id I don't know how to use this to get the record id.

I hope that makes sense.

What I am wondering is what do I use to get the ID of the record from the focused tab regardless of whether it is a primary or sub-tab?


If I understand your question correctly, you want to get the record ID out of a tab via a button. But you don't necessarily know if the button will be in a primary tab or sub tab at the time it's clicked.

I would suggest a combination of getEnclosingTabID and getPageInfo methods. You feed the enclosing tab ID into the getPageInfo method which in turn gives you the info about that tab in a JSON format.


var getRecordId = function getRecordId(result) { 
    var tabId = result.id;
    sforce.console.getPageInfo(tabId, doSomething);   

var doSomething = function doSomething(result) {    
    var parsedInfo = JSON.parse(result.pageInfo);
    var myObjectId = parsedInfo.objectId; 



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.