Closing sub tab and refreshing primary tab

I was working in this scenario, I did not see any snippets available in the community. Thought I would give a try and make it to work. I was successful to do it so. Let me give you guys a working code which might help you to save a lot of time.

Hope it helps.

Components:
1) JS button for Account layout
2) VF page
3) Controller

Scenario:
• When user opens a account record in a servicer console. It will open the record in a primary tab.
• There will be a detail page button which will host a VF page with custom control
• VF page will do some DML operations on SF objects including Account
• Now when user clicks a button in the VF page, then the VF page(SubTab) should close and Account Page(PrimaryTab) should refresh to show the updated data.

Code snippets

JS Button:
/* Require the Salesforce Console Integration Toolkit */
{!REQUIRESCRIPT("/support/console/33.0/integration.js")}
/* Verify sforce.console now exists */
console.log("sforce: %o", sforce);
/* Get the enclosing tab's ID, so that openSubtab()
can be called with the correct parameter*/
sforce.console.getEnclosingPrimaryTabId(
function(response) {
/* Remember the primary tab ID for openSubtab() */
var primaryTabId = response.id;
console.log("primaryTabId: %o", primaryTabId);
var recId = "{!Account.Id}";
/* Define the target URL using a dummy Case ID */
var url = "/apex/ServiceConsoleExp?id="+recId ;
/* Define other parameters for openSubtab() */
var isActive = true;
var tabLabel = null;
var subtabId = null;
/* Call openSubtab() */
sforce.console.openSubtab( primaryTabId, url, isActive, tabLabel, subtabId);
});
view raw 2016_02.js hosted with ❤ by GitHub
VF page:

<apex:page Controller="ServiceConsoleExp_C_ORG">
<apex:form id="theForm">
<apex:commandButton action="{!doFinish}" value="Click here to refresh a primary tab by id - CB" onComplete="closeTab(); return false;" />
</apex:form>
<apex:includeScript value="/support/console/22.0/integration.js"/>
<apex:includeScript value="/soap/ajax/27.0/apex.js"/>
<script type="text/javascript">
function closeTab() {
console.log('init');
if (sforce.console.isInConsole()) {
sforce.console.getEnclosingPrimaryTabId(showTabId);
}
}
var showTabId = function showTabId(result)
{
var tabId = result.id;
sforce.console.refreshPrimaryTabById(tabId , true, refreshSuccess);
};
var refreshSuccess = function refreshSuccess(result) {
if (result.success == true) {
console.log('2');
sforce.console.getEnclosingTabId(closeSubtab);
} else {
//any code
}
};
var closeSubtab = function closeSubtab(result) {
var tabId = result.id;
console.log('1');
sforce.console.closeTab(tabId);
};
</script>
</apex:page>
view raw 2016_02.htm hosted with ❤ by GitHub
Controller:

public class ServiceConsoleExp_C_ORG {
public Id recId;
public ServiceConsoleExp_C_ORG() {
recId = (Id)ApexPages.currentPage().getParameters().get('id');
System.debug('---'+ recId);
}
public PageReference doFinish(){
System.debug('---'+ recId);
Account a = [SELECT ID, name FROM Account WHERE id=:recId];
a.name = a.name + ':)';
update a;
return null;
}
}
view raw 2016_02.java hosted with ❤ by GitHub

Comments

  1. Hello Pradeep,

    My name is Sudheer and need your help can you please email me at skondamuri@gmail.com

    Thanks

    Sudheer

    ReplyDelete
  2. Just curious what if i have to use NavigatetoURL(External) with similar approach

    ReplyDelete
  3. Hi,
    i need help on this which im doing similar task....please
    contact me email-srishapyati4@gmail.com

    ReplyDelete

Post a Comment