What you will learn?
- Get Editable fields of any standard or custom object
- Fetch all updatable fields in Apex
- Get Label & Api Name of writable fields
Demo: Generic apex method to get updatable fields
In Salesforce we have Read Only, Editable, Required fields.
Read Only fields: Formula fields, System Fields like: Created Date, Last Modified Date etc.
Editable Fields: A custom or standard fields which user can modify if they have right access.
Required Fields: The fields which cannot be empty while creating records.
Apex code to get editable/updatable fields
public static Map<String,String> getUpdatableFields(String strObjectName) {
Map<String, Schema.SObjectType> detail = Schema.getGlobalDescribe();
Map<String,String> mapOfUpdatableFielde = new Map<String,String>();
for(Schema.SObjectField fields :detail.get(strObjectName).getDescribe().fields.getMap().Values()) {
If(fields.getDescribe().isAccessible() && fields.getDescribe().isCreateable() && fields.getDescribe().isUpdateable()) {
mapOfUpdatableFielde.put(fields.getDescribe().getLabel() , fields.getDescribe().getName());
System.debug(fields.getDescribe().getLabel());
}
}
return mapOfUpdatableFielde;
}
String objectName = 'Account';
getUpdatableFields(objectName);
We are using isCreateable() & isUpdateable() methods to identify writable fields. This code will always return some standard read-only fields like: OwnerId which may not be updated from LWC lightning-record-edit-form.