+ Start a Discussion
OzZiE90OzZiE90 

Custom, dynamic highlight panel

Hi all,

I am attempting to create a custom highlight panel. What I ultimately need to do is on the focus of a subtab, I need an event to fire and send information to the highlight panel. The highlight panel is for the primary tab (Account), and the event needs to fire from the subtab (Opportunity). The reason I need to do it like this is because we pull in information via an outside API (which is displayed on the subtab on a VF page), and when a subtab is focused, I need the fired event to send this information so the primary tab highlight panel can grab it.

I am aware of the sforce.console.onFocusedSubtab() method, however, the issue with this is that the event will fire for every subtab that is open. So if 3 opporutnity subtabs are open for one account, when one of those subtabs is focused, all 3 events will be fired from all 3 subtabs.

Does anyone have any suggestions how I can go about getting the event to fire only from the focused subtab?

Thanks in advance, and any suggestions/comments would be much appreciated.
scottbcovertscottbcovert
Hi OzZiE90,

Perhaps you could make use of the getFocusedSubtabId() method in conjunction with the onFocusedSubtab() callback to ensure the event is fired only from the focused subtab.
OzZiE90OzZiE90
Hi scottbcovert,

That is a great idea, unfortuneately I already attempted to do some logic to only fire the event using the 
getFocusedSubtabId() in conjunction with onFocusedSubtab() (I probably should have mentioned that). The thing about that is when onFocusedSubtab() is called, it gets called from every tab. So when getFocusedSubtabId() gets called after, the result from onFocusedSubtab() will already contain the ID from the currently focused tab. So when getFocusedSubtabId(), it will contain the same ID.

Quoted from SF:   
onFocusedSubtab() response:  "The ID of the subtab on which the browser is focused."
scottbcovertscottbcovert
Hi OzZiE90,

It sounds like the onFocusedSubtab() method is not behaving as documented unless I'm missing something-how could multiple tabs simultaneously have focus? Perhaps you can try to use a global javascript variable as a flag to prevent the duplicate firing, though I suppose if the actions are firing in parallel this might not help.
OzZiE90OzZiE90
Well, unfortunately the onFocusedSubtab() is behaving properly, or as documented.

Quote from SF for method:
"Registers a function to call when the focus of the browser changes to a different subtab."
 
So basically any time a new subtab is focused, the event/method registered to onFocusedSubtab() gets fired. I don't think a global variable would work since they are firing in parallel. One unfortunate thing that the sforce console does not have is a tab focus console event for event listeners. So, they have event listeners for tab open/close, IE: 
sforce.console.addEventListener(sforce.console.ConsoleEvent.CLOSE_TAB, function());
 
What I need is something like:
sforce.console.addEventListener(sforce.console.ConsoleEvent.TAB_FOCUS, function());
scottbcovertscottbcovert
Maybe I just haven't played around with the console in too long. I was thinking if a user clicks a sub tab then that tab becomes the focused sub tab, firing the onFocusedSubtab callback function. If he/she then clicks a different sub tab, then that becomes the focused sub tab and the onFocusedSubtab callback function fires again. You're saying that clicking any sub tab calls the callback function N number of times where N is equal to the total number of open sub tabs? That seems odd to me; isn't the concept of focus singular to a particular element?

Are you able to share any more of the code for your console? I wonder if there is something else we're missing that is causing the conflict because I don't believe the onFocusedSubtab callback function should fire for all open sub tabs when focus changes, but rather only once with a response for the newly focused sub tab.
OzZiE90OzZiE90
This is correct scott, it will fire N amount of times. I couldn't agree more, it is very weird, but that's how it behaves. I can definitely provide code... which I should have done earlier  :P

window.onload = function(){
sforce.console.onFocusedSubtab(fireEvent);
}

var fireEvent = function(result){
var oppID = '{!Opportunity.Loan_Number__c}';
sforce.console.fireEvent( 'oppLoad', oppID);
}
 
The even listener grabbing this event will print out a message with numbers... If I have two subtabs open, the console log will look like this...
 
message from real time 8952375
message from real time 1520493
 
 
scottbcovertscottbcovert
OzZiE90,

Sorry for the delayed response-out of curiosity do you see this behavior in all browsers? Sometimes javascript issues can be browser-related so it might be a good idea to test out all the usual suspects (Chrome, Firefox, & IE) to see if the problem is really a result of some browser incompatibility. You may very well end up having to open a case with Salesforce, but in the meantime you could try using a setTimeout to possibly help your cause like below:
 
var delay = (function(){
    var timer = 0;
    return function(callback, ms){
        clearTimeout(timer);
        timer = setTimeout(callback, ms);
    };
})();

var fireEvent = function(result){
    delay(function(){
        var oppID = '{!Opportunity.Loan_Number__c}';
        sforce.console.fireEvent('oppLoad',oppId);
    }, 300);
}

window.onload = function(){
    sforce.console.onFocusedSubtab(fireEvent);
}

This is intended to ensure the sforce.console.fireEvent method is fired only once, though you may need to adjust the delay-hope this helps!
OzZiE90OzZiE90
Hi scottbcovert,

Sorry for the delayed reponse as well... It was not a browser issue (PS I did try the timeout previously and it did not work, great suggestion though). I actually ended up opening a case with Salesforce and they confirmed what I had originally thought (the event will fire from every tab no matter what tab gets focused).

Currently, there is no work around to force Salesforce to only fire from the actual focused tab. I eneded up having to change a few things in my UI to get what I wanted.

Thank you for your responses!
scottbcovertscottbcovert
Hi OzZiE90,

Happy to help, but sorry it wasn't more useful. Bummer that you had to rework your approach but I guess sometimes that is the lowest-hanging fruit, certainly in this case given the response from the support team. Happy coding!