[SalesForce] Visualforce page commandbutton to open link in a Salesforce tab (Lightning)

I have a

 <apex:commandButton id="ViewAll" value="View All" onclick="viewAll()" />  

with so far

<script>
    function viewAll(){            
       console.log('param is {!Account.Id}')
       window.open("../apex/ActiveSubscriptionsSecond?id={! Account.Id }")
    }
</script>

I wanted to open the VF page not in a new window but in a Salesforce tab (for the Lightning experience)

Sample

Best Answer

You can create a lightning component that uses the openTab method from the workspaceAPI javascript object. The docs actually have an example lightning component code that shows a button that opens a new tab. https://developer.salesforce.com/docs/atlas.en-us.api_console.meta/api_console/sforce_api_console_lightning_opentab.htm

If you want to keep your VF page, you can embed the lightning component on it. That is explained here: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/components_visualforce.htm

Of course, this may require some bigger changes to your VF page. And if you tried to keep the UI changes small, the button may not look exactly like the rest of the page. (Though you could try some styling options.) If that's a huge undertaking, then there are options if you want to be more creative. For instance, you could create a lightning component that receives an event and upon receiving the event it opens the new tab. This would be very similar code to the stock openTab example above. And then in your VF page, you could send your custom component an event when your VF page button is clicked. Here’s how you do events like that between VF and lightning components: https://developer.salesforce.com/blogs/developer-relations/2017/01/lightning-visualforce-communication.html