I have an issue. My requirement is this, I have a VF page which opens from a custom button located on a Case Detail page. The VF Page has a CommandLink and a CommandButton which performs certain update to the Case Owner field on the Case detail page. The VF Page is opened as a Sub tab to a Case Primary tab on the console.

After updating the changes to the owner field on the case detail, I have to refresh my Primary Tab(Case Detail) and along with that i have to close the Sub tab which contains the VF Page. I find a weird issue here, My sub tab is getting closed first and the Primary tab refreshes after sub tab is closed. So what happens now is wierd for me, When the refresh of the primary tab happens it opens the closed VF Page again(that is the sub tab is reopened). I want to close the sub tab after the save has happened on the primary tab and refresh success.

Here is my code,


<apex:page standardController="Case" extensions="CIAssignProjectController" name="Assign Project">
    <apex:includeScript value="/support/console/34.0/integration.js"/>

    <script type="text/javascript">

        function setBeforeUnloadEvent(flag) {
            console.log('getting primary tab id');
            console.log('getting current subtab id');
                //First find the ID of the current tab to close it

        function refreshTabsAfterSave () {
            console.log('getting link');
            sforce.console.getTabLink(sforce.console.TabLink.PARENT_AND_CHILDREN, currentPrimaryTabId, refreshPrimaryTab);

        var refreshPrimaryTab = function refreshPrimaryTab (res) {
            console.log('tab link is: ', res);
            sforce.console.openConsoleUrl(currentPrimaryTabId, res.tabLink, true, [], [], afterRefresh);

        var currentSubTabId; // set on page load
        var currentPrimaryTabId; // set on page load

        var getPrimaryTabId = function getPrimaryTabId (result) {
            console.log('primary tab id is: ', result.id);
            currentPrimaryTabId = result.id;

        var getSubtabId = function getSubtabId (result) {
            console.log('sub tab id is: ', result.id);
            currentSubTabId = result.id;

    var afterRefresh = function afterRefresh (res) {
            console.log('after refresh, get enclosing tab');

        var getEnclosingTab = function getEnclosingTab (res) {
            console.log('enclosing tab is: ', res.id);

        function doCloseTab() {
        var closeSubtab = function closeSubtab(result) {
            var tabId = result.id;


    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock title="Work Assignment">
            <apex:pageBlockSection Title="User Data" columns="1" >
                <apex:pageBlockTable id="UserSkillsTable" value="{!MatchingUserSkills}" var="ms" rendered="{!IF(AND(NOT(ISBLANK(MatchingUserSkillsdata)),MatchingUserSkillsdata.size > 0), true, false)}" >
            <!-- A number of columns goes here -->

                    <apex:column style="width:7%">
                        <apex:facet name="header">Assign Project</apex:facet>

            <!-- This is where i Update the Owner of the Case field -->

                        <apex:commandLink status="loadingStatus" immediate="true" action="{!Assignrecord}" oncomplete="setBeforeUnloadEvent('{!pageswitch}');" value="Assign"> 
                            <apex:param name="Assignrowid" value="{!ms.MatchingUserId}"/> 

            <apex:pageBlockButtons location="bottom">
                <apex:commandButton status="loadingStatus" action="{!AutoAssignrecord}" value="Auto Assignment" oncomplete="setBeforeUnloadEvent('{!pageswitch}');" /> 
                <apex:commandButton status="loadingStatus" onclick="doCloseTab();" value="Back" />


My Class: (Only Button Logic I have added)

public class CIAssignProjectController {

    Public Boolean pageswitch {get;set;}
    private Case cs {get;set;}
    Private case c1;

    public CIAssignProjectController (ApexPages.StandardController controller) {
        cs =(Case) controller.getRecord();
        pageswitch = false;

    // No Problem with this code, As this is getting the Owner Field updated perfectly
    public pagereference Assignrecord(){ 

        if(cu.Profile.Name == 'CI User'){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,system.label.Projectusererrormessage));
            return null;
        // Some Codes for Update runs here , No errors on this found

                update c1;
                pageswitch = true;
                    update lstTask; 

                //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Confirm,system.label.CI_Assign_Project));
                //PageReference updatepageRef = new PageReference('/' + cs.id);
                //return updatepageRef; 

                return null; // I return Null because, If i call PageReference it opens the case detail in the Same Sub tab.
            catch(DmlException ex){
                return null;        


MY Button Code : (Execute javaScript)

var caseId = '{!Case.Id}'; 

var s = caseId.toString(); 
var returnMessage = sforce.apex.execute("AssignProject","checkSLABreach",{recid:s}); 

if (sforce.console.isInConsole()) { 
sforce.console.openSubtab(result.id, '/apex/CIAssignProject?id=' + caseId, true, 'Assign Project: {!Case.CaseNumber}', null); 
else { 
window.open('/apex/CIAssignProject?id=' +caseId); 

The main problem that i guess is because the afterRefresh callback function on OpenConsoleURL is called first and then the refresh happens. I tried to give setTimeOut(function(){afterRefresh},5000); on openConsoleUrl, But it is not working for me.

  • Just wondering -- is one of these refresh calls not actually required ? That is, does the console fire the refreshes for you post your DML operations without your refresh function ? – CloudHugger Jan 6 '16 at 9:14
  • DML operation reflects only when we explicitly add a Refresh function. Coz my PageReference to my VF Page is NULL and even if i add a PageReference to my Case detail page, It refreshes in the same Sub tab and the Detail section of the primary tab remains unaffected. Manually by refreshing the page we can see the updated value. So I want to close my sub tab and refresh my primary tab after DML operation is succeeded. – AjaySFDC Jan 6 '16 at 9:37
  • If I understand the problem correctly, i believe i have seen this behaviour before in console app. Dml on your primary record will fire a primary tab refresh. Consider conditionally popping the subtab at page load only if an attribute has been set on case detail. When you update the owner field , first close the subtab then set the new subtab attribute such that the primary tab javascript decides not to rerender the subtab after DML update. – CloudHugger Jan 6 '16 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.