Old thread, but posting this for others unaware of the other solutions in this use case, and some general debugging tips for this type of issue.
The method used in the question should work, but is missing some parameters like the callback function for success and the override subtabid (or null if not overriding an existing subtab). This page is also using API 20 of the console kit which is fairly old at this point (not as much at original post, but still old).
Also, you can do some debugging in the openSubtab callback with console log to double check that the result coming to the openSubtab callback is containing a primary tab id. If that Id is null, I believe you'll see a brand new primary tab opened up (as you see here).
Example with openSubtab with all necessary params, updated console integration kit (API 36), and a sample console.log statement:
<apex:includeScript value="/support/console/36.0/integration.js"/>
<script type="text/javascript">
function testOpenSubtab() {
//First find the ID of the primary tab to put the new subtab in
sforce.console.getEnclosingPrimaryTabId(openSubtab);
}
var openSubtab = function openSubtab(result){
var primaryTabId = result.id;
// good place to debug to see if we received a primary tab id
if (console && console.log) {
console.log(result);
}
sforce.console.openSubtab(primaryTabId , 'https://na1.salesforce.com/apex/service_cloud_console_flow_1', true,
'Add New Service',null,openSuccess);
}
var openSuccess = function openSuccess(result) {
//Report whether we succeeded in opening the subtab
if (result.success == true) {
alert('subtab successfully opened');
} else {
alert('subtab cannot be opened');
}
};
</script>
<button type="button" onclick="testOpenSubtab();">Test Subtab</button>
Additionally there are two other ways to change tabs in VF with the srcUp and srcSelf functions. srcUp is used to open a subtab and srcSelf is used to change the current tabs location. These two methods are very useful for javascript custom buttons as well.
You can find a lengthy example from one of the PMs of service cloud console Gautam Vasudev in his descriptive post, on the idea exchange for some reason...
https://success.salesforce.com/ideaView?id=08730000000YKxYAAW
Because the srcUp function is only available in the context of console you'll want to check for whether the function exists to determine if you're in that context, otherwise handle appropriately for your VF being hosted externally (call window.open for example).
Here is a brief example:
var url = 'http://someurl.com'; // or {!$Page.somevf}, or urlfor($action.... etc
if (typeof(srcUp) == 'function') {
srcUp(url);
} else {
window.open(url); // open new browser tab if not in console context
}
I finally was able to get this code working...
The documentation does not explain this well in my opinion. Basically, you create a variable, in this case called refreshPrimaryTab
which is assigned to a function which will retrieve your primaryTabId and assign it to your request for a refresh. This though will not execute yet. In order to execute, you need to call a salesforce function. This is done by calling sforce.console.getEnclosingPrimaryTabId()
with your variable inside. As the example shows it will look like: sforce.console.getEnclosingPrimaryTabId(refreshPrimaryTab);
Below is a simple example of how from a sub tab you can get your primary tab to refresh.
var refreshPrimaryTab = function showTabId(result) {
var tabId = result.id;
sforce.console.refreshPrimaryTabById(tabId, true);
};
function doPrimaryTabRefresh() {
sforce.console.getEnclosingPrimaryTabId(refreshPrimaryTab);
}
doPrimaryTabRefresh();
Best Answer
Here's a VF page I created that opens subtabs immediately. Then I added the VF page to the Console as one of the side bars (so it can execute). I hope the example helps!
My Controller: