+ Start a Discussion
Big EarsBig Ears 

Console Integration Toolkit - IsInConsole() doesn't always know that it's in the console....

Dear all,

I have an inline visualforce page on a record with a command button. The command button takes some actions on the controller and then refreshes the record. This page was originally created by another developer and we've since moved to using the Service Console (but not for all users).

The command button took some action on the controller side to update the record and then conditionally rendered an output panel at the top of the page which prompted a refresh of the whole record. If the user is in the service console, this doesn't work correctly and the user is exited from the service console.

I'd like the command button to refresh the record when the user is in the service console, but I'm falling at the first hurdle. The IsInConsole() function from the console integration toolkit seems to think that the record isn't being displayed within the console when the commandButton is clicked:
<apex:page standardController="XXXX" extensions="XXXX">
    <apex:includeScript value="/support/console/30.0/integration.js"/>
	<apex:outputPanel rendered="{!refreshPage}">
      <script type="text/javascript">
        
         if (sforce.console.isInConsole()) {
            alert("in console");
            **SOME JAVASCRIPT TO REFRESH THE TAB**
        } else {
            alert("not in console");
            window.top.location = '/{!Tenancy__c.id}';
        }
      </script>
    </apex:outputPanel>
    <apex:form id="featuresForm">
       <apex:pageBlock >
        <script>
        	if (sforce.console.isInConsole()) {
        		alert("in console");
        	} else {
        		alert("not in console");
        	}
        </script>
        <apex:pageMessages />
        <apex:pageBlockButtons >
            <!--<apex:commandButton value="Save" action="{!save}" rendered="{!Tenancy__c.LockFeatures__c}"/>-->
        </apex:pageBlockButtons>
        ***MORE VF PAGE HERE***

I've put in place some "debugging" javascript which confirms that ehen the page first loads, the alert pops up with the "In Console" message. So I know the toolkit is working and properly invoked when the record is first loaded up. However, when the "CommandButton" is clicked and the inline VF page refreshes, the alert that pops up, then says "not in console". 

Has anybody seen this before and know why the toolkit may not recognise the console?

Alternatively - Feel free to suggest ways to re-engineer the page so that the refresh will work in both the console and non-console view. We do need the refresh, so that new information is displayed to the user, otherwise I could just get away with an inline refresh of the VF page.

With thanks,
Best Answer chosen by Big Ears
Big EarsBig Ears
Ok, got it! I put the following at the top of the page:
<apex:includeScript value="/support/console/30.0/integration.js"/>
<script type="text/javascript">
   function testIsInConsole(){
      if (sforce.console.isInConsole()) {
         //alert("in console - will attempt to refresh subtab");
         sforce.console.getEnclosingTabId(refreshSubTab);
      } else {
         //alert("not in console - will attempt to refresh the full page");
         window.top.location = '/{!Tenancy__c.id}';
      }
   }
 
   var refreshSubTab = function refreshSubTab(result) {
      //alert("Got the id - "+result);
      var subTabId = result.id;
      sforce.console.refreshSubtabById(subTabId, true);
   }
         
</script>
I then had the Commandbuttons include an "onComplete" attribute to call the function "testIsInConsole()" which then checked for the subtab name and refreshed it.

I'm not sure why the code wasn't working in my previous incarnations, but I think it had to do with the following:
  • I was declaring my "refreshSubTab" variable within my if-else statement
  • I had been using an "onClick" attribute, so the page was refreshing before the record updates had taken place in my apex code
Ashish - Thanks for giving me the idea to use the link to trigger the javascript. It let me differentiate between issues with the Commandbuttons and issues with my javascript (which is where all the issues lay!)

All Answers

Ashish_SFDCAshish_SFDC
Hi,


Try again and check if it was a temporary issue,

Also try using the condition like this,

Sample Code–Visualforce
<apex:page standardController="Case">
<A HREF="#" onClick="testIsInConsole();return false">
Click here to check if the page is in the Service Cloud console</A>
<apex:includeScript value="/support/console/22.0/integration.js"/>
<script type="text/javascript">
function testIsInConsole() {
if (sforce.console.isInConsole()) {
alert('in console');
} else {
alert('not in console');
}
}
</script>
</apex:page>
Note: This example is set to run by clicking a custom link on a case. For more information, see “Defining Custom
Buttons and Links” in the Salesforce online help.
Response
Returns true if the page is in the Salesforce console; false if the page is not in the Salesforce console.


Regards,
Ashish
Big EarsBig Ears
Ashish,

Thanks for the pointer - The code you've supplied correctly identifies that the embedded VF page is within the console.

I've moved the code around so that the javascript to refresh the page is explictly called by the Command button, rather than implied through the refresh of the outputpanel in lines3-14 and that seems to have solved 1 issue - The  user is no longer directed out of the console. However, the refresh prompted by the Command button seems to perpetuate the issue, with a pop-up indicating that the inline VF page is not in the console.

The issue seems to be that the use of the Command button, which refreshes the page, makes the inline Visualforce page think it is not within the Console and, therefore, not refresh properly. Do you know why this may be?

Andy
Big EarsBig Ears
Actually - Further to my last message - This may not be the issue. The javascript I've put in place doesn't seem to refresh the page record, regardless of whether the user is in the console or in the "normal" UI.
Big EarsBig Ears
Ok, got it! I put the following at the top of the page:
<apex:includeScript value="/support/console/30.0/integration.js"/>
<script type="text/javascript">
   function testIsInConsole(){
      if (sforce.console.isInConsole()) {
         //alert("in console - will attempt to refresh subtab");
         sforce.console.getEnclosingTabId(refreshSubTab);
      } else {
         //alert("not in console - will attempt to refresh the full page");
         window.top.location = '/{!Tenancy__c.id}';
      }
   }
 
   var refreshSubTab = function refreshSubTab(result) {
      //alert("Got the id - "+result);
      var subTabId = result.id;
      sforce.console.refreshSubtabById(subTabId, true);
   }
         
</script>
I then had the Commandbuttons include an "onComplete" attribute to call the function "testIsInConsole()" which then checked for the subtab name and refreshed it.

I'm not sure why the code wasn't working in my previous incarnations, but I think it had to do with the following:
  • I was declaring my "refreshSubTab" variable within my if-else statement
  • I had been using an "onClick" attribute, so the page was refreshing before the record updates had taken place in my apex code
Ashish - Thanks for giving me the idea to use the link to trigger the javascript. It let me differentiate between issues with the Commandbuttons and issues with my javascript (which is where all the issues lay!)
This was selected as the best answer