function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
xoomsterxoomster 

How can you tell if current session is Service Cloud console from Apex controller class?

Hi all,

Base on my code below, how can I programmatically decide whether or not the current user session is in the service cloud console?

I would like the user to be able to got to this link and have it render in console or regular; https://cs7.salesforce.com/apex/Launcher?txnID=a0AM0000004P7Se

................
Page (Page name = Launcher)
................
<apex:page controller="LauncherController" action="{!processRequest}">

</apex:page>
................
Controller
................
public class LauncherController{
    private String transactionID{get;set;}

    public LauncherController(){

    transactionID = ApexPages.currentPage().getParameters().get('txnID');

    }//end-method

    public PageReference processRequest(){ 

    PageReference pageRef;  

    // if console

    pageRef = new PageReference('https://cs7.salesforce.com/console#%2F' + transactionID);

    // else

    //pageRef = new PageReference('https://cs7.salesforce.com/' + transactionID);

    // end-if

    if(transactionID==null){
      return pageRef;
    }
    return pageRef;
    }
}
Best Answer chosen by xoomster
Shailesh DeshpandeShailesh Deshpande
I havent tested, but i dont think you might even need a contoller for this. You can simply use javascript to achieve the same.
<apex:page>

   <apex:includeScript value="/support/console/22.0/integration.js"/>
      
    <script type="text/javascript">
        function testIsInConsole() {
            
            var txnId = '{!$CurrentPage.parameters.txnId}' ;
             alert(txnId) ;
            if (sforce.console.isInConsole()) {
                  var loc = '../console#%2F' + txnId;                 
                  window.open(loc, "_self");                                 
               } else {
                 var loc = '../' + txnId;                 
                 window.open(loc, "_self");                                           
            }
        }
        
        testIsInConsole();
             
    </script>
</apex:page>

Thanks,
Shailesh.

All Answers

BalajiRanganathanBalajiRanganathan
In the link, you need to add one more param and decide using that param. for example

https://cs7.salesforce.com/apex/Launcher?txnID=a0AM0000004P7Se&source=console
https://cs7.salesforce.com/apex/Launcher?txnID=a0AM0000004P7Se&source=other
xoomsterxoomster
Thanks for your comment.  However, I'll still need the "IF" statement to decide which URL link use.
Shailesh DeshpandeShailesh Deshpande
Yes we can identify the context. You need to connect to the Salesforce Console Integration Toolkit in your page and have javascript to determine the context. We have a method called isInConsole(). Below is an example from Salesforce Console Integration Toolkit guide:
<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>
You could use a variable in your class and set its value based on whatever is returned by isInConsole() method. 

Thanks,
Shailesh.
 
xoomsterxoomster
Thanks Shailesh. How can I include this example in my code without using "Onclick" but instead on page load?
Shailesh DeshpandeShailesh Deshpande
I havent tested, but i dont think you might even need a contoller for this. You can simply use javascript to achieve the same.
<apex:page>

   <apex:includeScript value="/support/console/22.0/integration.js"/>
      
    <script type="text/javascript">
        function testIsInConsole() {
            
            var txnId = '{!$CurrentPage.parameters.txnId}' ;
             alert(txnId) ;
            if (sforce.console.isInConsole()) {
                  var loc = '../console#%2F' + txnId;                 
                  window.open(loc, "_self");                                 
               } else {
                 var loc = '../' + txnId;                 
                 window.open(loc, "_self");                                           
            }
        }
        
        testIsInConsole();
             
    </script>
</apex:page>

Thanks,
Shailesh.
This was selected as the best answer
xoomsterxoomster
Thanks Shailesh.  Yes, this code works .. however, I had to hardcode the console location variable to 

var loc = 'https://cs7.salesforce.com/console#%2F' + txnId; 

instead of 

var loc = '../console#%2F' + txnId;   <-- this link only opens up the home page of console.

-tony