0

I have a lightning web component that displays accounts and contacts and allows navigation to them with the help of a button. I have used the LWC in lightning experience as well as in classic using lightning out. I used if(UserContext === 'Theme4t' || UserContext === 'Theme4d') to identify whether I am in Lightning or in classic. I will navigate based on UserContext. Problem is, I am getting UserContext as undefined. Hence I am not able to navigate in both lightning as well as in classic.

I passed the user context from the VF page as follows:

<script>
    var myUserContext = "{!$User.UITheme}";
    $Lightning.use("c:lwcInVisualforceapp", function() {
        $Lightning.createComponent("c:wrapperClassTestNavigation", {
            UserContext: myUserContext 
        },
        "lwcDemo",
            function(component) {
            }
       );
    });
    </script>

I access UserContext in javascript as follows:

import { LightningElement,api,track, wire } from 'lwc';
import getWrapperLis from '@salesforce/apex/WrapperClass.getWrapperList';
import { NavigationMixin } from 'lightning/navigation'; ///Navigation

export default class WrapperClassTestNavigation extends NavigationMixin(LightningElement) {
    @api recordId;
    @track columns = columns;
    @api UserContext;
    @wire(getWrapperLis,{}) wrappers;

    navigateToRecordViewPage(event,UserContext) {
        this.record = event.detail.row;
        if(UserContext !== undefined) {
            if(UserContext === 'Theme4t' || UserContext === 'Theme4d') {
                this[NavigationMixin.Navigate]({
                    type: 'standard__recordPage',
                    attributes: {
                        recordId: this.record.id,
                        actionName: 'view'
                    }
                });
            } else {
                window.location.assign('/'+this.record.id);
            }
        }
    } 
}

HTML:

<template>
    <lightning-card title="LWC Wrapper Component" icon-name="standard:contact">
        <div style="height: 400px;">
            <lightning-datatable
                    key-field="Id"
                    data={wrappers.data}
                    columns={columns}
                    onrowaction={navigateToRecordViewPage}
                    hide-checkbox-column="true">
            </lightning-datatable>
        </div>    
    </lightning-card>
</template>
0

There is no need to use UserContext. Just replace the javascript function with this code:

navigateToRecordViewPage(event) {
        this.record = event.detail.row;

        if(document.referrer.indexOf(".lightning.force.com") > 0){
            this[NavigationMixin.Navigate]({
                type: 'standard__recordPage',
                attributes: {
                    recordId: this.record.id,
                    actionName: 'view'
                }
            });
        }else{
            window.location.assign('/'+this.record.id);
        }
    }
| improve this answer | |

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.