I have requirement that i need to open the list of cases associated to an account in a sub tab by clicking on a button in account detail page. I have written the following visualforce page to accomplish this task. As of now, I have hardcoded the id values. Could anyone one please tell me how to open multiple subtabs at a time for a primary tab in console ?
<apex:page standardController="Case">
<A HREF="#" onClick="callMultipleSubTabs();return false">
Click here to open a new subtab</A>
<apex:includeScript value="/support/console/22.0/integration.js"/>
<script type="text/javascript">
var strURL;
var strTitle;
function callMultipleSubTabs()
{
var caseIds = "5009000000VxOsE;5009000000VxOsD";
var caseTitle = "00001001;00001000";
var id = caseIds.split(";");
var title = caseTitle.split(";")
var finalId = caseIds.replace(";","|");
for(i=0;i<id.length ; i++)
{
if(sforce.console.isInConsole())
{
alert('In console');
testOpenSubtab(caseId,caseNumber);
}
else
{
window.open("/"+id[i],'_blank');
window.focus();
}
}
}
function testOpenSubtab(inputURL,inputTitle) {
strURL = inputURL;
strTitle = inputTitle;
//First find the ID of the primary tab to put the new subtab in
sforce.console.getEnclosingPrimaryTabId(openSubtab);
}
var openSubtab = function openSubtab(result) {
//Now that we have the primary tab ID, we can open a new subtab in it
var primaryTabId = result.id;
sforce.console.openSubtab(primaryTabId , strURL, false,
strTitle, null, openSuccess, strTitle);
};
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>
Best Answer
You're trying to pass your variables by leaking them into the global state. Bad idea. Basically:
Don't do that, it's bad practice. And in this case, it's not even going to work, because the variables are synchronous in
function1
and asynchronous infunction2
. So the first step in getting your code working is to pass those variables to theopenSubtab
method.However, you don't even need to make each call asynchronous. You can perform the asynchronous logic on page load.
Now you can remove that first line (and parameter) from
openSubTab
and make these calls synchronously on click.So back to your loop. You are trying to pass a url of
caseId
, but that is not even a valid url. Make sure you prepend a/
character to make it a relative path. I would recommend using the$Action
variable but that might be overly complicated in this script. Since eventually you will be iterating aList<Case>
, I recommend designing your function to simply accept aCase
record. Something like: