April 30, 2021

How to open records as sub tab from Visualforce in Salesforce Lightning?

Sample Code:
<apex:page standardController="Account">
    <apex:includeScript value="/support/console/51.0/integration.js"/>
    <script type="text/javascript">
    
        var recordIdForSubTab;
        var recordNameForSubTab;
        
        function openAsSubTab( recId, recName ) {
                    
            recordIdForSubTab = recId;
            recordNameForSubTab = recName;
            sforce.console.getEnclosingPrimaryTabId( openSubtab );
            
        }
        
        var openSubtab = function openSubtab( result ) {
        
            var primaryTabId = result.id;
            sforce.console.openSubtab( primaryTabId , '/' + recordIdForSubTab, false,
                recordNameForSubTab, null, openSuccess, 'salesforceSubtab' );
                
        };
        
        var openSuccess = function openSuccess( result ) {
            if (result.success == true) {
                alert( 'Subtab successfully opened' );
            } else {
                alert( 'Subtab cannot be opened ' + JSON.stringify( result ) );
            }
        };
        
    </script>
     <apex:pageBlock >
        <apex:pageblockTable value="{!Account.Contacts}" var="con">
            <apex:column value="{!con.Name}"/>
            <apex:column ><apex:outputLink onclick="openAsSubTab( '{!con.Id}', '{!con.Name}' );">Open As Sub Tab</apex:outputLink></apex:column>
        </apex:pageblockTable>
    </apex:pageBlock>
</apex:page>


Output:


3 comments:

  1. Thanks you so much. It helps

    ReplyDelete
  2. Hi, I am using the same code but this visualforce page is in a lightning component, I am getting 'Uncaught DOMException: Blocked a frame with origin "https://mck--uat.my.salesforce.com" from accessing a cross-origin frame.'. Any idea on how to resolve it?

    ReplyDelete
    Replies
    1. Check whether this is helpful - https://salesforce.stackexchange.com/questions/204480/openprimarytab-fails-with-salesforce-com-url

      Delete