function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dnyaneshwar KakadDnyaneshwar Kakad 

How to detect a required field in field set through Apex.

Is there any way to determine which field is required at fieldset level in Apex
VinayVinay (Salesforce Developers) 
Hi Dnyaneshwar,

Check below references to detect a required field in field set through apex.

https://salesforce.stackexchange.com/questions/229684/how-to-detect-a-required-field-from-a-fieldset
https://sfdctrails.com/2021/10/04/how-to-identify-required-fields-of-object-using-apex/

Please mark as Best Answer if above information was helpful.

Thanks,
Arun Kumar 1141Arun Kumar 1141
Hi Dnyaneshwar,

You can try the below code for checking the required field in field set through Apex

Schema.FieldSet myFieldSet = Schema.SObjectType.YourObjectname__c.fieldSets.YourFieldSetName;
Set<String> requiredFields = new Set<String>();
for (Schema.FieldSetMember fieldSetMember : myFieldSet.getFields()) {
    String fieldName = fieldSetMember.getFieldPath();
    Schema.DescribeFieldResult fieldDescribe = YourObjectname__c.SObjectType.getDescribe().fields.getMap().get(fieldName).getDescribe();
    if (!fieldDescribe.isNillable() && fieldDescribe.isCreateable()) {
        requiredFields.add(fieldName);
    }
}
System.debug('Required Fields in Field Set: ' + requiredFields);

OR

You can check the below reference for more info
https://sfdctrails.com/2021/10/04/how-to-identify-required-fields-of-object-using-apex/

Mark this as best answer if this helps.
Thanks