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:


1 comment: