0% found this document useful (0 votes)
17 views2 pages

LWC

Uploaded by

rjohar369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

LWC

Uploaded by

rjohar369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

To control the visibility of an LWC component based on user profiles, you can use

either Custom Metadata or Custom Settings. Here's an overview of both approaches:

1. Using Custom Metadata


Step-by-Step Approach:
Create Custom Metadata:

Create a custom metadata type, e.g., LWC_Config.


Add two fields:
Active__c: Checkbox (to activate or deactivate the configuration).
Profile_Names__c: Text Area (to store profile names separated by semicolons, e.g.,
System Admin;Test).
Populate Custom Metadata:

Populate the Profile_Names__c field with the profiles that should have access to
the LWC component.
Apex Controller:

Write an Apex method to check if the current user's profile matches any of the
profiles listed in the custom metadata. Here’s an example:
apex
Copy code
public with sharing class LWCVisibilityController {
@AuraEnabled(cacheable=true)
public static Boolean shouldDisplayComponent() {
// Get the current user's profile name
String profileName = [SELECT Profile.Name FROM User WHERE Id
= :UserInfo.getUserId()].Profile.Name;

// Get the custom metadata records


LWC_Config__mdt config = [SELECT Profile_Names__c FROM LWC_Config__mdt
WHERE Active__c = true LIMIT 1];

// Split the profile names by semicolon


List<String> profileNames = config.Profile_Names__c.split(';');

// Check if the current profile is in the allowed list


return profileNames.contains(profileName);
}
}
LWC Component:

Use the @wire decorator in LWC to call the Apex method and control the visibility
of the component.
javascript
Copy code
import { LightningElement, wire } from 'lwc';
import shouldDisplayComponent from
'@salesforce/apex/LWCVisibilityController.shouldDisplayComponent';

export default class MyComponent extends LightningElement {


displayComponent = false;

@wire(shouldDisplayComponent)
wiredShouldDisplayComponent({ error, data }) {
if (data) {
this.displayComponent = data;
} else if (error) {
console.error(error);
}
}
}
Template:

Conditionally render the component based on displayComponent.


html
Copy code
<template>
<template if:true={displayComponent}>
<div>
<p>This content is only visible to specific profiles.</p>
</div>
</template>
</template>
2. Using Hierarchical Custom Settings
Step-by-Step Approach:
Create Hierarchical Custom Setting:

Create a hierarchical custom setting, e.g., Profile_Access_Settings.


Add a checkbox field, Has_Access__c.
Set Up Profile-Based Values:

Set the Has_Access__c field to true for profiles that should have access to the
component.
Apex Controller:

Write an Apex method to check the custom setting for the current user's profile:
apex
Copy code
public with sharing class LWCVisibilityController {
@AuraEnabled(cacheable=true)
public static Boolean shouldDisplayComponent() {
Profile_Access_Settings__c accessSettings =
Profile_Access_Settings__c.getInstance(UserInfo.getProfileId());
return accessSettings != null && accessSettings.Has_Access__c;
}
}
LWC Component and Template:

The approach here is the same as in the custom metadata method: call the Apex
method using @wire and conditionally render the component based on the response.
Conclusion:
Both approaches are effective for controlling LWC component visibility based on
profiles. The choice between Custom Metadata and Custom Settings may depend on
whether you need hierarchical access (Custom Settings) or a more flexible, reusable
configuration (Custom Metadata).

You might also like