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
msantanamsantana 

How to pass data(Id, etc) from the Console Primary Object to controller. AKA Custom Console Component or Footer widget

This is my first time developing a Custom Console Component.
Situation: I created a VF page that got added to a Custom Console Component in order to open as a Footer widget in the console.Footer widget view

This is what I need to do: The VF page should do: When you open a Case in the Console as a Primary TAB and the field__c is empty then the VF page should display Case.field1__c(lookup) in order update the case field when a Save button is click, to then later find the data from a related Object and display this data within VF page with or without the previously rendered filed that we just updated.

If the mentionded field already has a value then it would display the data from a related Object.

The PROBLEM that I am having:
    Not able to pass the CASE data from my currently open case in the Console as Primary tab to my custom Controller in order to find the data from a related Object.

My VF page:
<apex:page standardController="Case" extensions="CaseConsoleControllerExt2"> 
    <apex:includeScript value="/support/console/42.0/integration.js"/>
    <script type="text/javascript">
        //sforce.console.onCustomConsoleComponentButtonClicked(sforce.console.getFocusedPrimaryTabObjectId(response));
        function testGetFocusedPrimaryTabObjectId() {
            sforce.console.getFocusedPrimaryTabObjectId(showObjectId);
        }
        var showObjectId = function showObjectId(result) {
            //Display the object ID
            alert('Object ID: ' + result.id);
        };
    </script>
    <apex:pageMessages /><br/>
    
    <apex:form >
        <apex:pageBlock title="Edit Case" mode="edit">
            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!Case.CaseNumber}"/> 
<!-- Not able to get data from the primary tab to display CaseNumber, ID, ETC -->
                <apex:repeat value="{!editableFields}" var="f">
                    <apex:inputField value="{!Case[f]}"/>
                </apex:repeat>
           </apex:pageBlockSection>
           <apex:pageBlockButtons>
               <apex:commandButton action="{!save}" value="Save"/>
           </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
    <A HREF="#" onClick="testGetFocusedPrimaryTabObjectId();return false">
     Click here to get the focused primary tab object ID</A>
</apex:page>
My Custom controller:
public class CaseConsoleControllerExt2 {
	private final Case MyCase;
    
    public CaseConsoleControllerExt2(ApexPages.StandardController stdController) {
        //this.editCase = [Select Id, CaseNumber,SAP_Customer__c FROM Case Where Id = :ApexPages.currentPage().getParameters().get('id') ];
        // this.MyCase = (Case)ApexPages.currentPage().getParameters().get('id');
        this.MyCase = (Case)stdController.getRecord(); // ALWAYS NULL
        system.debug('My current Case is: '+MyCase);  // ALWAYS NULL
        //stdController.addFields(editableFields);
    }
    
    public Case MyCase() {
        return MyCase;
    }

    
	public List<String> editableFields {
        get {
            if (editableFields == null) {
                editableFields = new List<String>();
                editableFields.add('SAP_Customer__c');
            }
            return editableFields ;
        }
        private set;
    }
}

My questions:
  1. Why my controller in not getting the data from my current record in the primary tab when ​doing this.MyCase = (Case)stdController.getRecord();
  2. How can I pass data from VF page script (Case data) to the controller since the I am getting the case Id with the script sforce.console.getFocusedPrimaryTabObjectId
  3. Any problem with my controller?
Best Answer chosen by msantana
Jesus Ledezma 5Jesus Ledezma 5
Hi MSantana, I took a quick look at your code but I am not sure it can work, well, at least with the code you sent, the extension in your Visualforce Page is "VFP_CustomConsoleComponentCtrl" and your controller name is "CaseConsoleControllerExt". 

In any case, I copy your code to my environment and had to modify it a bit because I couldn't even save it, it general I just made some minor changes and it worked. 

In reference to your concerns about what's the best way to do it, well, it depends on your requirements in terms of security, accessibility, process flow, environment (Classic/Lightning Experience), etc. 

I am adding a couple of screenshots to confirm with you, at least, a part of your requirements:

In the first screenshot, I selected a case and clicked on the bottom console button to open the popup screen. You can see the data of the case in the Primary Tab is displayed in the popup screen:
User-added image

Then, I clicked on a second case. My code doesn't close the popup window but just refresh the data in it:
User-added image

If that's what you want as behavior, this is the code:
-Visualforce Page
<apex:page StandardController="Case" Extensions="CaseConsoleControllerExt" showHeader="false" sidebar="false">
    <apex:includeScript value="/support/console/42.0/integration.js"/>
    <script type="text/javascript">
        if(sforce.console){

            var redrawForm = function (result) {
            
                assignVar(result.objectId);
               
            }    



            function GetPrimaryTabObjectId() {
                sforce.console.getFocusedPrimaryTabObjectId(showObjectId);
                }
            var showObjectId = function showObjectId(result) {
                //Display the object ID
                assignVar(result.id);
                alert('Object ID: '+result.id);
            };
            var closewidget= function (result) {
                sforce.console.setCustomConsoleComponentVisible(false);
            };
            function refreshFocusTabId(id) 
            {
            if (sforce.console.isInConsole())
                sforce.console.getFocusedPrimaryTabId(showTabId);                
            else
                window.top.location.href = '/' + id;
            }

            var showTabId = function showTabId(result) 
            {
                var tabId = result.id;
                alert(tabId);
                sforce.console.refreshPrimaryTabById(tabId , true);
            };
            sforce.console.onCustomConsoleComponentButtonClicked(GetPrimaryTabObjectId);
            sforce.console.onFocusedPrimaryTab(redrawForm);
        }
    </script>
    <apex:form id="frm">
        <apex:actionFunction name="assignVar" rerender="frm" action="{!returnVar}">
                <apex:param name="cid" value="" assignTo="{!consoleId}"/>
        </apex:actionFunction><br/><br/>
        Case Number : {!c.CaseNumber}<br/><br/>
        Case Status : {!c.Status}<br/><br/>
        Case Subject : <apex:inputField value="{!c.Subject}" /><br/><br/>
        <center><apex:commandButton value="Update Case" action="{!updateCase}" onComplete="refreshFocusTabId('{!consoleId}')"/> </center>
    </apex:form>    
</apex:page>
-Controller:
public with sharing class CaseConsoleControllerExt {
   public Id consoleId { get; set;}
   public Case c{get;set;}
   public void returnVar() {// This is where you get the param sent from the VF page
        System.debug('consoleId ' + consoleId);
        c = new Case();
        c = [Select Id,CaseNumber,Subject,Case_Subject__c,Status from Case where id =: consoleId];

   }
  public CaseConsoleControllerExt(ApexPages.StandardController controller) {
        // Do thonthing here
        System.debug('##### In constructor ####');
        c = new Case();
  }
  public void updateCase() {
        update c;
  }
}

I modified just a bit of your code, that way you can continue from there.

Hope that helps. 
JL
 

All Answers

Jesus Ledezma 5Jesus Ledezma 5
Hi, sorry, I might haven't fully understood the issue, but here you have some code that could help.

If you want to pass a parameter to your controller with the popup console you can use this code:
I tried to get something similar to your code,
This is the Visualforce page
<apex:page showHeader="false" sidebar="false" controller="MyCaseController" >
    <apex:includeScript value="/support/console/42.0/integration.js"/>
    <script type="text/javascript">

    var redrawForm = function (result) {
        
        rerenderForm(result.objectId);
           
        }    
        sforce.console.onFocusedPrimaryTab(redrawForm);
 
    </script>

    <apex:form id="caseform">

        <apex:actionFunction action="{!MyCaseController}" name="rerenderForm" reRender="CaseBlock">
            <apex:param id="MyCase" name="MyCase" value=""/> 

        </apex:actionFunction>    

        <apex:pageBlock id="CaseBlock" title="Edit Case" mode="edit">
            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!CaseinPage.CaseNumber}"/> 
<!-- Not able to get data from the primary tab to display CaseNumber, ID, ETC 
                <apex:repeat value="{!editableFields}" var="f">
                    <apex:inputField value="{!Case[f]}"/>
                </apex:repeat>-->
           </apex:pageBlockSection>
<!--           <apex:pageBlockButtons>
               <apex:commandButton action="{!save}" value="Save"/>
           </apex:pageBlockButtons> -->
        </apex:pageBlock>
    </apex:form>
</apex:page>

This is the controller:
 
public with sharing class MyCaseController {

    public String  MyCase {get;set;}
    public case CaseinPage {get;set;}
    
    public PageReference MyCaseController() {
        MyCase =Apexpages.currentPage().getParameters().get('MyCase');
        System.debug('The value MyCase in Controller is: ' + MyCase );
        CaseinPage = [select id ,CaseNumber from Case where id = :MyCase];
        // this.MyCase = (Case)ApexPages.currentPage().getParameters().get('id');
        //this.MyCase = (Case)stdController.getRecord(); // ALWAYS NULL
        //stdController.addFields(editableFields);
        return null;
    }
    
    public MyCaseController() {

    }

}

Let me know if that works for you. 

JL 
 
msantanamsantana
Sorry Jesus but it didn't work. I got a blank value in "Mycase" varaible.

This is what it has been working for me so far:
<apex:page StandardController="Case" Extensions="VFP_CustomConsoleComponentCtrl" showHeader="false" sidebar="false">
    <apex:includeScript value="/support/console/42.0/integration.js"/>
    <script type="text/javascript">
        if(sforce.console){
            function GetPrimaryTabObjectId() {
                sforce.console.getFocusedPrimaryTabObjectId(showObjectId);
                }
            var showObjectId = function showObjectId(result) {
                //Display the object ID
                assignVar(result.id);
                alert('Object ID: '+result.id);
            };
            var closewidget= function (result) {
                sforce.console.setCustomConsoleComponentVisible(false);
            };
            function refreshFocusTabId(id) 
            {
            if (sforce.console.isInConsole())
                sforce.console.getFocusedPrimaryTabId(showTabId);                
            else
                window.top.location.href = '/' + id;
            }

            var showTabId = function showTabId(result) 
            {
                var tabId = result.id;
                alert(tabId);
                sforce.console.refreshPrimaryTabById(tabId , true);
            };
            sforce.console.onCustomConsoleComponentButtonClicked(GetPrimaryTabObjectId);
            sforce.console.onFocusedPrimaryTab(closewidget);
        }
    </script>
    <apex:form id="frm">
        <apex:actionFunction name="assignVar" rerender="frm" action="{!returnVar}">
                <apex:param name="cid" value="" assignTo="{!caseId}"/>
        </apex:actionFunction><br/><br/>
        Case Number : {!c.CaseNumber}<br/><br/>
        Case Status : {!c.Status}<br/><br/>
        Case Subject : <apex:inputField value="{!c.Subject}" /><br/><br/>
        <center><apex:commandButton value="Update Case" action="{!updateCase}" onComplete="refreshFocusTabId('{!caseId}')"/> </center>
    </apex:form>    
</apex:page>
Controller:
public with sharing class CaseConsoleControllerExt {
   public Id consoleId { get; set;}
   public Case c{get;set;}
   public void returnVar() {// This is where you get the param sent from the VF page
        System.debug('consoleId ' + consoleId);
        c = new Case();
        c = [Select Id,CaseNumber,Subject,Case_Subject__c,Status from Case where id =: consoleId];
   }
  public CaseConsoleControllerExt(ApexPages.StandardController controller) {
        // Do thonthing here
        System.debug('##### In constructor ####');
        c = new Case();
  }
  public void updateCase() {
        update c;
  }
}

Eventhoguht that I am able to get the case Id in the controller I am not sure if this is the best way to able to populate the information back of the case in the VF page.  I thought I would need to use psgeReference to redirect the page to 
return new ApexPages.Standardcontroller(caseEdit).view();



 
Jesus Ledezma 5Jesus Ledezma 5
Hi MSantana, I took a quick look at your code but I am not sure it can work, well, at least with the code you sent, the extension in your Visualforce Page is "VFP_CustomConsoleComponentCtrl" and your controller name is "CaseConsoleControllerExt". 

In any case, I copy your code to my environment and had to modify it a bit because I couldn't even save it, it general I just made some minor changes and it worked. 

In reference to your concerns about what's the best way to do it, well, it depends on your requirements in terms of security, accessibility, process flow, environment (Classic/Lightning Experience), etc. 

I am adding a couple of screenshots to confirm with you, at least, a part of your requirements:

In the first screenshot, I selected a case and clicked on the bottom console button to open the popup screen. You can see the data of the case in the Primary Tab is displayed in the popup screen:
User-added image

Then, I clicked on a second case. My code doesn't close the popup window but just refresh the data in it:
User-added image

If that's what you want as behavior, this is the code:
-Visualforce Page
<apex:page StandardController="Case" Extensions="CaseConsoleControllerExt" showHeader="false" sidebar="false">
    <apex:includeScript value="/support/console/42.0/integration.js"/>
    <script type="text/javascript">
        if(sforce.console){

            var redrawForm = function (result) {
            
                assignVar(result.objectId);
               
            }    



            function GetPrimaryTabObjectId() {
                sforce.console.getFocusedPrimaryTabObjectId(showObjectId);
                }
            var showObjectId = function showObjectId(result) {
                //Display the object ID
                assignVar(result.id);
                alert('Object ID: '+result.id);
            };
            var closewidget= function (result) {
                sforce.console.setCustomConsoleComponentVisible(false);
            };
            function refreshFocusTabId(id) 
            {
            if (sforce.console.isInConsole())
                sforce.console.getFocusedPrimaryTabId(showTabId);                
            else
                window.top.location.href = '/' + id;
            }

            var showTabId = function showTabId(result) 
            {
                var tabId = result.id;
                alert(tabId);
                sforce.console.refreshPrimaryTabById(tabId , true);
            };
            sforce.console.onCustomConsoleComponentButtonClicked(GetPrimaryTabObjectId);
            sforce.console.onFocusedPrimaryTab(redrawForm);
        }
    </script>
    <apex:form id="frm">
        <apex:actionFunction name="assignVar" rerender="frm" action="{!returnVar}">
                <apex:param name="cid" value="" assignTo="{!consoleId}"/>
        </apex:actionFunction><br/><br/>
        Case Number : {!c.CaseNumber}<br/><br/>
        Case Status : {!c.Status}<br/><br/>
        Case Subject : <apex:inputField value="{!c.Subject}" /><br/><br/>
        <center><apex:commandButton value="Update Case" action="{!updateCase}" onComplete="refreshFocusTabId('{!consoleId}')"/> </center>
    </apex:form>    
</apex:page>
-Controller:
public with sharing class CaseConsoleControllerExt {
   public Id consoleId { get; set;}
   public Case c{get;set;}
   public void returnVar() {// This is where you get the param sent from the VF page
        System.debug('consoleId ' + consoleId);
        c = new Case();
        c = [Select Id,CaseNumber,Subject,Case_Subject__c,Status from Case where id =: consoleId];

   }
  public CaseConsoleControllerExt(ApexPages.StandardController controller) {
        // Do thonthing here
        System.debug('##### In constructor ####');
        c = new Case();
  }
  public void updateCase() {
        update c;
  }
}

I modified just a bit of your code, that way you can continue from there.

Hope that helps. 
JL
 
This was selected as the best answer
Jesus Ledezma 5Jesus Ledezma 5
Hi, again sorry for the delay, the code I've sent you works in my environment, but in any case, I took yours and tried to execute, however, I am a bit surprised it works in your environment, the extension class is not even the name of the controller you sent. 

Your concerns about the "best way" of doing this depend on different factors, ex. your environment (Classic / Lightning), your security needs, accessibility requirements, etc, this is because there are a couple of ways to get to your objective (if I understood it properly)

Look, I took your code and modified a bit to make it run and this is what I got:

-Selected an existing case and opened the bottom consoled widget. The case info is displayed in the window:
Selected an existing case and opened the bottom consoled widget. The case info is displayed in the window.

-Then, I selected another existing case and the information in the window changed accordingly. I didn't want the popup window to close automatically, it could be a bit annoying for the user, so I left it open.
User-added image

If this is your intended behavior, this is the code for that. Again, I just took your code and modified it slightly.

Visualforce Page:
<apex:page StandardController="Case" Extensions="CaseConsoleControllerExt" showHeader="false" sidebar="false">
    <apex:includeScript value="/support/console/42.0/integration.js"/>
    <script type="text/javascript">
        if(sforce.console){

            var redrawForm = function (result) {
            
                assignVar(result.objectId);
               
            }    



            function GetPrimaryTabObjectId() {
                sforce.console.getFocusedPrimaryTabObjectId(showObjectId);
                }
            var showObjectId = function showObjectId(result) {
                //Display the object ID
                assignVar(result.id);
                alert('Object ID: '+result.id);
            };
            var closewidget= function (result) {
                sforce.console.setCustomConsoleComponentVisible(false);
            };
            function refreshFocusTabId(id) 
            {
            if (sforce.console.isInConsole())
                sforce.console.getFocusedPrimaryTabId(showTabId);                
            else
                window.top.location.href = '/' + id;
            }

            var showTabId = function showTabId(result) 
            {
                var tabId = result.id;
                alert(tabId);
                sforce.console.refreshPrimaryTabById(tabId , true);
            };
            sforce.console.onCustomConsoleComponentButtonClicked(GetPrimaryTabObjectId);
            sforce.console.onFocusedPrimaryTab(redrawForm);
        }
    </script>
    <apex:form id="frm">
        <apex:actionFunction name="assignVar" rerender="frm" action="{!returnVar}">
                <apex:param name="cid" value="" assignTo="{!consoleId}"/>
        </apex:actionFunction><br/><br/>
        Case Number : {!c.CaseNumber}<br/><br/>
        Case Status : {!c.Status}<br/><br/>
        Case Subject : <apex:inputField value="{!c.Subject}" /><br/><br/>
        <center><apex:commandButton value="Update Case" action="{!updateCase}" onComplete="refreshFocusTabId('{!consoleId}')"/> </center>
    </apex:form>    
</apex:page>

Controller:
public with sharing class CaseConsoleControllerExt {
   public Id consoleId { get; set;}
   public Case c{get;set;}
   public void returnVar() {// This is where you get the param sent from the VF page
        System.debug('consoleId ' + consoleId);
        c = new Case();
        c = [Select Id,CaseNumber,Subject,Case_Subject__c,Status from Case where id =: consoleId];

   }
  public CaseConsoleControllerExt(ApexPages.StandardController controller) {
        // Do thonthing here
        System.debug('##### In constructor ####');
        c = new Case();
  }
  public void updateCase() {
        update c;
  }
}