+ Start a Discussion
Jessica PuckettJessica Puckett 

Get Id from Console View?

Hello everyone,
We have a Visualforce page calling an Apex Class which currently obtains the record Id from the URL.  We're attempting to implement Console and this process no longer works in Console View.  I'm trying to understand how to replace the referheader with the getEnclosingPrimaryTabObjectId() function for Console, but I keep coming up short.  Any advice would be appreciated!  Here's the existing code:
public with sharing class TaskSidebarExtension {
  public Task tsk {get;set;}
  public String refer {get;set;}
  public Boolean valid {get;set;}
  public String parentId {get;set;}
  public String recordName {get;set;}
  public String phoneNumber {get;set;}
  public string redirectUrl {get;set;}
  
  public TaskSidebarExtension(ApexPages.standardController ctl) {
    this.tsk = (Task)ctl.getRecord();
    
  }
  
  public void findRecord() {
    valid = true;
    try {      
      //check the referer header (send via the iFrame) and extract the record id
      String referHeader = ApexPages.currentPage().getHeaders().get('referer');
      if (referHeader != null) {
        refer = referHeader;
      }
      
      if (refer != null) {
        Integer startPos = refer.lastIndexOf('/')+1;
        Integer endPos = refer.indexOf('?');
        if (endPos < 0) endPos = refer.length();
        
        parentId = refer.substring(startPos,endPos);
      }
      
    
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Hi Jessica,

You should check it in your visualforce page that your page is opened in Classic or Console and then get the record id.
<apex:includeScript value="/support/console/40.0/integration.js"/>
<script type="text/javascript">
if(sforce.console.isInConsole()){
function testGetEnclosingPrimaryTabObjectId() {
sforce.console.getEnclosingPrimaryTabObjectId(showObjectId);
}
var showObjectId = function showObjectId(result) {
'{!recordId}' = result.id;
// Display the object ID
alert ('Object ID: ' + result.id);
};
}
</script>
Jessica PuckettJessica Puckett
Jessica Puckett
Hi @SalesFORCE_enFORCEr!

Thanks for the comment!  I'm still confused because the Visualforce page is using the Apex class as an extension and pulling values from that like this:
<tr><td>Phone:&nbsp;<apex:outputText value="{!phoneNumber}"/></td></tr>
                <tr><td>Caller ID:&nbsp;<apex:outputText value="{!recordName}"/></td></tr>
Since it's getting the values from the Apex Class {!phoneNumber}, etc. I was wondering if I could use the sforce.console.getEnclosingPrimaryTabObjectId function within the Class instead of on the Visualforce page?  Here is my thought process:
public with sharing class TaskSidebarExtension {
  public Task tsk {get;set;}
  public Boolean valid {get;set;}
  public String parentId {get;set;}
  public String recordName {get;set;}
  public String phoneNumber {get;set;}
  public string redirectUrl {get;set;}
  public string result {get;set;}

public function testGetEnclosingPrimaryTabObjectId(){
sforce.console.getEnclosingPrimaryTabObjectId(showObjectId);
}
var showObjectId = function showObjectId(result) {

  public TaskSidebarExtension(ApexPages.standardController ctl) {
    this.tsk = (Task)ctl.getRecord();
    
  }
  
  public void findRecord() {
    valid = true;        
        parentId = result.id;
      }

However, I'm not sure where to reference the toolkit within the Apex Class...  Am I way off track?