Hello friends, In this post you will see an example that helps you to understand, how to use custom setting in lightning web component(lwc).
Custom setting in lightning web component
By using custom setting you can access cached data stored on salesforce platform in lighting web component. Let's see an example.
CustomSettingHandler.apxc
public class CustomSettingHandler {CustomSetting.html
@AuraEnabled(cacheable=true)
public static DemoCustomSetting__c getCustomSettings(){
return DemoCustomSetting__c.getOrgDefaults();
}
}
<template>CustomeSetting.js
<lightning-card variant="Narrow" title="Custom Setting in LWC" icon-name="standard:account">
<div class="slds-align_absolute-center">
<span>Name : <b>{myCustomSettings.data.Name__c}</b></span>
</div>
</lightning-card>
</template>
import { LightningElement, wire } from 'lwc';CustomSetting.js-meta.xml
import getCustomSettings from '@salesforce/apex/CustomSettingHandler.getCustomSettings';
export default class CustomSetting extends LightningElement {
@wire(getCustomSettings) myCustomSettings;
doSomething() {
let a = this.myCustomSettings.data.Name__c;
// do something...
}
}
<?xml version="1.0"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>51.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordAction</target>
</targets>
</LightningComponentBundle>
Output:
Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.
3 Comments
above code is not working as Cannot read properties of undefined (reading 'Name__c')
ReplyDeleteYou have to created a custom field inside custom setting (Name__c) then it will work for you and custom setting must be hierarchy type
ReplyDeleteHow to give the permission to user for this custom seeting field.
ReplyDeletePost a Comment