I am trying to create a link dynamically using the below code:

'<a href="/'+ row.id+'" target="_blank">'+ row.name+

The created link works well in normal view. It redirects to the record detail page.

Now, the problem I am facing is, to open a subtab, when the link is being accessed from Console View.

I have gone through the openSubtab documentation. But didn't work for me.

This is the code by referring the documents:

function openSubTab(recId){
             var redirectUrl = recId;
             if (sforce.console.isInConsole()) {
                         sforce.console.openSubtab(primaryResult.id, redirectUrl, true, '', null);
             } else {

                 window.top.location.href = '/'+redirectUrl


Link Generation Code(Added onClick):

'<a href="#" onClick = "openSubTab('+row.Id+');return false"" class="easyui-linkbutton" target="_blank">'+ row.name+

Is there a better way to do what I am trying to accomplish?

Note: The record ID would be dynamic.

  • 3
    onclick is case sensitive and mixing JavaScript and HTML is quite error prone. Added an answer which I feel to be more elegant.
    – Raul
    Oct 16, 2017 at 12:55

1 Answer 1


I like using jQuery on click listener to override navigation for the links based on context.

Here is an example-

<apex:page standardController="Case">

    <!-- Placeholder for link -->
    <div id="link"></div>

    <apex:includeScript value="//code.jquery.com/jquery-3.2.1.min.js"/>
    <apex:includeScript value="/support/console/40.0/integration.js"/>

        j$ = jQuery.noConflict();

        j$(document).ready(function() {

            // start - link generation code
            var caseId = "{!Case.Id}";
            var caseNumber = "{!Case.CaseNumber}";
            var spanHtml = '<span>' +
                    '<a class="open-case" href="/' + caseId + '" target="_blank">' + 
                        caseNumber +
                    '</a>' +
            // stop- link generation code

            // adding listner for links
            j$( "a.open-case" ).on( "click", function(e) {

                // stop default navigation

                var recordUrl = j$( this ).attr("href");
                var tabLabel = j$( this ).attr("text");

                if (sforce.console.isInConsole()) {
                    // navigate for console
                    callOpenSubtab(recordUrl, tabLabel);
                } else {
                    // redirect manually for non console view
                    window.location = recordUrl;
                // add more navigations maybe for salesforce1 or lightning as required

        // console method for opening tab
        function callOpenSubtab(recordUrl, tabLabel) {

            var openSuccess = function(result) {
                //Report whether we succeeded in opening the subtab
                if (result.success == true) {
                    alert('subtab successfully opened');
                } else {
                    alert('subtab cannot be opened');

            sforce.console.getEnclosingPrimaryTabId(function(result) {
                //Now that we have the primary tab ID, we can open a new subtab in it
                var primaryTabId = result.id;
                sforce.console.openSubtab(primaryTabId , recordUrl, false, 
                    tabLabel, null, openSuccess, 'salesforceSubtab');
  • Great! This opens the tab well. But however the control(focus) doesn't shift to the new tab,. It is opened, but I need to manually click the tab to view its details. Any specific handling to be done for this? Oct 16, 2017 at 13:09
  • switch 3rd parameter to true which indicates active. Refer documentation.
    – Raul
    Oct 16, 2017 at 13:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.