Get all required fields of Salesforce sObject.

Get all required fields of Salesforce sObject
Get all required fields of Salesforce sObject

In this article we will discuss how to find required fields of an Salesforce Object . The code given here to find required fields is applicable for Standard and Custom Objects as well .

There is no standard build in method available in Salesforce to find List of all required fields .

To demonstrate the concept we have created the Custom Object named as Student , Which is having one required field – *Age .

Find all Standard and Custom required Fields

Now In order to find the field is required or not we can use the isNillable() method . The following code will return all the Standard and Custom required fields.

				
					Map<String, Schema.SObjectType> describe = Schema.getGlobalDescribe();
Map<String, Schema.SObjectField> mapFields = describe.get('Student__c').getDescribe().fields.getMap();
for( String fieldName : mapFields.keySet() ) {
    Schema.DescribeFieldResult desribeResult = mapFields.get(fieldName).getDescribe();
    if(!desribeResult.isNillable()) {
        System.debug('Required Field '+  mapFields.get(fieldName));
    }   
}
				
			

Find all Custom required fields

				
					Map<String, Schema.SObjectType> describe = Schema.getGlobalDescribe();
Map<String, Schema.SObjectField> mapFields = describe.get('Student__c').getDescribe().fields.getMap();
for( String fieldName : mapFields.keySet() ) {
    Schema.DescribeFieldResult desribeResult = mapFields.get(fieldName).getDescribe();
    if(!desribeResult.isNillable() && !desribeResult.isDefaultedOnCreate() ) {
        System.debug('Required Field '+  mapFields.get(fieldName));
    }   
}
				
			

We hope this small post helped you . Please subscribe to our Youtube channel.

How the code works

The code gets all fields of Student__c and prints those where isNillable() is false, meaning the field cannot be empty. There is no built-in method that returns the required fields directly, so you combine the field describe properties.

A gap in the isNillable check

System fields such as Id, CreatedDate and LastModifiedDate are also not nillable, so they appear in the result even though you never fill them in. Some fields are also not nillable but get a default value automatically. A field is really required when you must provide it on create.

Better check for required fields

Map<String, Schema.SObjectField> mapFields = Schema.getGlobalDescribe().get('Student__c').getDescribe().fields.getMap();
for (String fieldName : mapFields.keySet()) {
    Schema.DescribeFieldResult d = mapFields.get(fieldName).getDescribe();
    if (d.isCreateable() && !d.isNillable() && !d.isDefaultedOnCreate()) {
        System.debug('Required field: ' + d.getName());
    }
}

This keeps only fields that can be set on create, cannot be empty, and have no automatic default. Replace Student__c with any standard or custom object.

FAQ

Does it include fields made required on the page layout?
No. Layout-required fields are not part of the describe result. Only fields required by the database definition appear.

Are validation rules included?
No. A validation rule that forces a value is not visible in the field describe.

Leave a Reply