[SalesForce] “worksaceAPI.openTab()” equivalent for lightning web components

How can the workspace API's "open tab" functionality be achieved with lightning web components? I've only seen this functionality described for aura components in the documentation.

Best Answer

Looks like official support for this is coming out soon, it was flagged for Summer 20 but I don't see support for it yet. https://help.salesforce.com/articleView?id=lex_roadmap.htm&type=5

In the mean time you can manually generate the URL needed to have LEX open the page in a subtab. Here is code for opening the new page for a custom object and passing in default field values.

    navTo(recId){

        let defaultValues = {
            'Parent__c' : recId
        }

        this.createObjInSubtab(defaultValues,'Custom_Obj__c');
    }

    createObjInSubtab(defaultValues,obj){

        let defaultValuesEncoded = encodeDefaultFieldValues(defaultValues);
        let subTabURL = '/lightning/o/'+obj+'/new?nooverride=1&count=2&defaultFieldValues=';
        subTabURL += defaultValuesEncoded+'&ws='+encodeURIComponent(window.location.pathname+'?count=1')+'';

        let temp = {
            type: 'standard__webPage',
            attributes: {
                url: subTabURL
            }
        };

        this[NavigationMixin.Navigate](temp);
    }