[SalesForce] Console component tries to open in subtab when primaryTabId is null

I am working on a visualforce console footer component that displays solutions. If a tab is open, the solution opens as a subtab. If no primary tab is open, the new page still tries to open in a subtab.

Iḿ using a simple if else statement to evaluate if primaryTabIdxRt === null || primaryTabIdxRt === undefined, but instead it still follows the else path. The console log clearly shows that primaryTabIdxRt is null, and it keeps going down the wrong path.

Here is the relevant function

<script type="text/javascript">
        function openSolution(recURL) {
            console.log('OpenSolutionInitial = ' + primaryTabIdxRt);
             if (primaryTabIdxRt === null || primaryTabIdxRt === undefined) {
                console.log('OpenInPrimaryTab = ' + primaryTabIdxRt);
                sforce.console.openPrimaryTab(null,recURL,true);
            } else {
                console.log('OpenInSubtab = ' + primaryTabIdxRt);
                sforce.console.openSubtab(primaryTabIdxRt,recURL,true); 
            }    
        }
</script>  

The function is called via onClick event

<a href="#" onclick="openSolution('{!instanceURL}/{!s.Id}');">{!s.SolutionName}</a>                 

Here is the console output when parent Id is null. Its going down the else path, even though the log shows that the Id is null before the if statement evaluates.

Console Log (id = null):

OpenSolutionInitial = null 
OpenInSubtab = null 
openSubTab: Invalid ID: null

Interestingly, the solution does open in primary tab when parent ID is undefined (which is the value when I first open or refresh the console), so its just null that isnt evaluating properly.

Console Log (id = undefined):

OpenSolutionInitial = undefined
OpenInPrimaryTab = undefined

Output:

opensInPrimaryTabWhenUndefined

I have tried evaluating == null, i have tried evaluating if(primaryTabIdxRt) and flipping the paths, I renamed primaryTabId to primaryTabxRt in case there was a naming conflict, I tried removing the || and using else if primaryTabxRt === undefined, and none of these had any impact.

Any ideas on what else to try? I have some other functions running to update primaryTabIdrXt when tab is closed, etc, and its all working reasonably well, but this piece which seems straightforward is not behaving.

Here is the code for the full visualforce page

<apex:page standardController="Solution" recordSetVar="sols">

<apex:variable value="{!LEFT($Api.Partner_Server_URL_260, FIND( '/services', $Api.Partner_Server_URL_260))}" var="instanceURL"/>

<apex:includeScript value="/support/console/39.0/integration.js"/>

<script type="text/javascript">
        var primaryTabIdxRt;
</script>

<script type="text/javascript">
        function refreshPrimaryTabId() {
            sforce.console.getFocusedPrimaryTabId(processTab);
            }
            var processTab = function processTab(result) {
                primaryTabIdxRt = result.id;
                console.log('primaryTabIdxRt RefreshTab  = '+ primaryTabIdxRt);
        }
        sforce.console.addEventListener(sforce.console.ConsoleEvent.CLOSE_TAB,
                                refreshPrimaryTabId);
</script> 

<script type="text/javascript">
        var eventHandler = function (result) {
            primaryTabIdxRt = result.id;
            console.log('primaryTabIdxRt onFocusedPT = ' + primaryTabIdxRt);
        }
        sforce.console.onFocusedPrimaryTab(eventHandler);
</script> 


<script type="text/javascript">
        function openSolution(recURL) {
            console.log('OpenSolutionInitial = ' + primaryTabIdxRt);
             if (primaryTabIdxRt === null) {
                console.log('OpenInPrimaryTabNull = ' + primaryTabIdxRt);
                sforce.console.openPrimaryTab(null,recURL,true);
                }
            else if (primaryTabIdxRt === undefined) {
                console.log('OpenInPrimaryTabUnd = ' + primaryTabIdxRt);
                sforce.console.openPrimaryTab(null,recURL,true);
                }
            else {
                console.log('OpenInSubtab = ' + primaryTabIdxRt);
                sforce.console.openSubtab(primaryTabIdxRt,recURL,true); 
            }    
        }
</script>  

        <apex:form >
            <apex:panelGrid >
                <apex:selectList value="{!filterId}" size="1" id="selList">
                <apex:actionSupport event="onchange" rerender="list"  />
                <apex:selectOptions value="{!listViewOptions}"></apex:selectOptions>
                </apex:selectList>
            </apex:panelGrid>

                <apex:dataList var="s" value="{!sols}" id="list" style="padding: 0px;">

           <a href="#" onclick="openSolution('{!instanceURL}/{!s.Id}');">{!s.SolutionName}</a> - {!s.Status}             

                </apex:dataList>                
           <apex:commandButton onclick="refreshPrimaryTabId();" title="ClickMe" value="Refresh Primary Tab ID" rerender="selList" />
        </apex:form>

</apex:page>

UPDATE

I modified the openSolution function to reflect the changes suggested by Santanu.

Here is the updated function. I changed from primaryTabIdxRt === null to primaryTabIdxRt == null, and I added window.location.reload();. For testing I also removed the lines that call openPrimaryTab and openSubtab methods, and am only using alerts to show which path is followed.

As the screenshots of the alerts illustrate, the function still is not following the null path when primaryTabIdxRt is null. So maybe I need to try a different way to evaluate null?

<script type="text/javascript">
        function openSolution() {
            alert('OpenSolutionInitial = ' + primaryTabIdxRt);
             if (primaryTabIdxRt == null) {
                alert('primary tab null ' + primaryTabIdxRt);
                }
            else if (primaryTabIdxRt === undefined) {
                alert('primary tab undefined ' + primaryTabIdxRt);
                }
            else {
                alert('open in subtab ' + primaryTabIdxRt);
            }
            window.location.reload();    
        }
</script>  

<a href="#" onclick="openSolution();">abcdefg</a>

Actions taken
1. open console
2. open console component
3. click 'refresh primary tab id' button
4. click on the link 'abcdefg'

see screenshots of the alerts below – its following the else / subtab path, instead of the else if / primaryTab path, even though it sees primary tab as null.

1. initial id is null

id is null

2. subtab path is followed

subtab path

Best Answer

I have taken your code done a small change and it is working as expected.

For shake of my testing, rather than solutions, I am opening a hardcoded Lead record.

Before clicking on Click Here url, I am clicking on 'Refresh Primary Tab Id` button so that it can recognize the tab id.

I think, every time your Solution record is opening on Subtab, may be in the Console App configuration you have chosen Solution to open as subtab.

That's why I have taken a lead record which is no way related to any tab defined in my Console App.

<apex:page standardController="Case">
    <apex:includeScript value="/support/console/29.0/integration.js"/>
    <script type="text/javascript">
        var primaryTabIdxRt;

        function refreshPrimaryTabId() {
            alert('inside refreshPrimaryTabId');
            sforce.console.getFocusedPrimaryTabId(processTab, true);
            }

            var processTab = function processTab(result) {
                primaryTabIdxRt = result.id;
                alert('primaryTabIdxRt=' + primaryTabIdxRt);

            }

            function testOpenPrimaryTab() {
                recURL = 'https://cs21.salesforce.com/00Qq0000004iHC2';  //lead id
                sforce.console.getFocusedPrimaryTabId(processTab, true);

                alert('OpenSolutionInitial = ' + primaryTabIdxRt);

                 if (primaryTabIdxRt == null || primaryTabIdxRt == 'null') {
                    alert('OpenInPrimaryTabNull = ' + primaryTabIdxRt);
                    sforce.console.openPrimaryTab(null,recURL,true);
                    }
                else if (primaryTabIdxRt == undefined) {
                    alert('OpenInPrimaryTabUnd = ' + primaryTabIdxRt);
                    sforce.console.openPrimaryTab(null,recURL,true);
                    }
                else {
                    alert('OpenInSubtab = ' + primaryTabIdxRt);
                    sforce.console.openSubtab(primaryTabIdxRt,recURL,true); 
                } 
                window.location.reload();
            }

    </script>
    <apex:form >
         <a href="#" onclick="testOpenPrimaryTab(); return false;">Click here</a>

      <apex:commandButton onclick="refreshPrimaryTabId(); return false;" title="ClickMe" value="Refresh Primary Tab ID" rerender="selList" />
    </apex:form>
</apex:page>

Update

When primary tab is null javascript actuals returns "null" String. So you have to check like this:

if (primaryTabIdxRt == null || primaryTabIdxRt == 'null')
Related Topic