Today I got a following requirement for a Community Cloud:
Can we have a link in a community header or navigation bar or menu, that will redirect user to its related Account? So that we show his Account details?
We couldn’t do a static link, as Account Id is always different.
For community users there are AccountId (and ContactId) fields on User object. So I thought it’s easy – I’ll get AccountId from User, and expose it via Lighting Component which will then make a redirect.
Community custom page has been already created – it was just about creating LWC and putting it there.
I started with Apex Class which gets Account Id for logged in user.
public with sharing class AccountRedirect { @AuraEnabled(Cacheable=true) public static String getAccountId() { User user = [ SELECT Id, AccountId FROM User WHERE Id = :UserInfo.getUserId() ]; return user.AccountId; } }
and then I created LWC for it. Template:
<template> Redirect in progress... </template>
and JS controller
import { LightningElement, api } from 'lwc'; import { NavigationMixin } from 'lightning/navigation'; import getAccountId from '@salesforce/apex/AccountRedirect.getAccountId'; export default class AccountRedirect extends NavigationMixin(LightningElement) { connectedCallback() { getAccountId() .then(result => { if (result !== undefined) { this[NavigationMixin.Navigate]({ type: 'standard__recordPage', attributes: { recordId: result, objectApiName: 'Account', actionName: 'view' } }); } }) .catch(error => { console.log('error', error); }); } }
and added tags to xml to make sure to expose it on a Community Page
<lightningcomponentbundle xmlns="http://soap.sforce.com/2006/04/metadata"> <apiversion>53.0</apiversion> <description>Account Redirect</description> <isexposed>true</isexposed> <masterlabel>Account Redirect</masterlabel> <targets> <target>lightningCommunity__Page</target> <target>lightningCommunity__Default</target> </targets> </lightningcomponentbundle>
I got an error saying a community profile didn’t have access to apex class. Once I granted access – it worked, user was redirected.
But then I had a thought:
Is it possible to do it only via LWC, without Apex code?
Then I could get rid of apex class, unit test and no need to modify the profile.
So I ended up with this js controller code:
import { LightningElement, wire } from 'lwc'; import { NavigationMixin } from 'lightning/navigation'; import { getRecord, getFieldValue } from "lightning/uiRecordApi"; import ACCOUNT_ID from "@salesforce/schema/User.AccountId"; import USER_ID from "@salesforce/user/Id"; export default class AccountRedirect extends NavigationMixin(LightningElement) { @wire(getRecord, { recordId: USER_ID, fields: [ACCOUNT_ID] }) wiredAccount({ error, data }) { if (data) { if (data !== undefined) { let accId = data.fields.AccountId.value; this[NavigationMixin.Navigate]({ type: 'standard__recordPage', attributes: { recordId: accId, objectApiName: 'Account', actionName: 'view' } }); } } else if (error) { console.log('error', error); this.error = error; } } }
I worked perfectly well:
- User clicked “Account Information”
- Which opened “/redirect” page
- Which contained our LWC
- Which actually made a redirect to account details page.