Skip to content

Instantly share code, notes, and snippets.

@arun12209
Created February 9, 2023 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arun12209/608a8b56dfa580bf81f5cccedcac56a6 to your computer and use it in GitHub Desktop.
Save arun12209/608a8b56dfa580bf81f5cccedcac56a6 to your computer and use it in GitHub Desktop.
import { LightningElement, wire, api, track } from 'lwc';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
// Importing required modules from the LWC library
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class CreateNewContact extends LightningElement {
// Defining the object API name
objectApiName = 'Contact';
// Defining the fields to be displayed
fields = ['FirstName', 'LastName', 'Email', 'Phone'];
// Declaring a trackable variable to store the contact fields data
@track contactFLS = {};
// Function to check the visibility of the fields
checkFieldVisibility(data, fieldName) {
// Checking if the field exists in the data
if (data.fields[fieldName] != undefined) {
// If exists, return true
return true;
} else {
// If not exists, return false
return false;
}
}
// Using the wire method to fetch the object information
@wire(getObjectInfo, { objectApiName: '$objectApiName'})
objectInfo({error, data}) {
// Handling the error
if (error) {
// Showing the toast message
const message = 'Error fetching object information: ' + error.message;
this.dispatchEvent(new ShowToastEvent({
title: 'Error',
message: message,
variant: 'error'
}));
}
// Checking if data is present
else if (data) {
// Iterating through the fields defined
this.fields.forEach(field => {
// Checking the visibility of the field
if (this.checkFieldVisibility(data, field)) {
// If visible, setting the visibility to true and checking if the field is updateable or not
this.contactFLS[`${field}Visible`] = true;
this.contactFLS[`${field}Disabled`] = !data.fields[field].updateable;
} else {
// If not visible, setting the visibility to false
this.contactFLS[`${field}Visible`] = false;
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment