3

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

2
  • Where are you setting primaryTabIdxRt?
    – Raul
    Feb 4, 2017 at 21:37
  • I've got listeners that are setting it when primary tab is opened or closed. That part seems to be working fine as if I open a case the solution opens as a subtab of case, if I open a new case it opens as a subtab of new case, etc, and logs are fine. I'll post the full page when I'm back home in case it's helpful.
    – gorav
    Feb 4, 2017 at 22:24

1 Answer 1

1

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')
5
  • Brilliant idea. Really thought that would do it but just checked and solutions set to open as primary tab. I removed the solutions object from the console app entirely and it still is behaving the same. Thx for testing w leads, that was a great approach.
    – gorav
    Feb 5, 2017 at 1:36
  • Please take this code and work on that, the way I have tested it is working Feb 5, 2017 at 1:50
  • thanks @santanu, I have tried to do so, and have added an update w/ the modified function, but its still behaving the same way for me. I stripped out all the functionality from the openSolution method and am just displaying alerts, but if primaryTabIdxRt is null, it does not follow the proper path (tried with == or with ===). I'm Wondering if i can try another way to evaluate null. My javascript is unfortunately very limited, using this exercise to learn!
    – gorav
    Feb 5, 2017 at 3:00
  • Please find the updated answer regarding 'null' checking, you have to check "null" String and it is working for me correctly. Please accept and close this question. Feb 5, 2017 at 3:21
  • checking for the string null in addition appears to have solved it. thanks for working through it - this was driving me crazy.
    – gorav
    Feb 5, 2017 at 3:35

You must log in to answer this question.

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