• #25485
    Salesforce | Ankit Ankit #657

    How to find all required fields of sobject in Salesforce?

    Hi all,

    Is there any way to find all required fields of a Sobject?




    Salesforce | Adarsh Adarsh #658

    Hi ankit,

    you may use following code.


    Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe();
    Schema.SObjectType s = m.get(‘Contact’);
    Schema.DescribeSObjectResult r = s.getDescribe();
    Map<String,Schema.SObjectField> fields = r.fields.getMap();

    for(String field : fields.keyset()) {
    Schema.DescribeFieldResult describeResult = fields.get(field).getDescribe();
    if (describeResult.isCreateable() && !describeResult.isNillable() && !describeResult.isDefaultedOnCreate()) {

    hope it helps 🙂

    Salesforce | PRANAV PRANAV #659

    Hi Ankit,

    here is no direct property available in Apex dynamic API to represent the required field. However there is another way to know about it.
    If any field have below three properties then it is mandatory field.

    • If it is Creatable
    • If it is not nillable and
    • If it does not have any default value

    Yo can use the below code for finding it out

    Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe() ;
    Schema.SObjectType s = m.get(‘sObjectName’) ; //give your object name in place of sObjectName
    Schema.DescribeSObjectResult r = s.getDescribe() ;
    Map<String,Schema.SObjectField> fields = r.fields.getMap() ;

    for(String f : fields.keyset())
    Schema.DescribeFieldResult desribeResult = fields.get(f).getDescribe();
    if( desribeResult.isCreateable() && !desribeResult.isNillable() && !desribeResult.isDefaultedOnCreate() )
    //This is mandatory / required field
    system.debug(‘///’ + f );

    Hope this helps you more.

    Salesforce | Archit Archit #25

    Schema.DescribeSObjectResult r = systemObjectType.getDescribe();
    Map<String,Schema.SObjectField> M = r.fields.getMap();
    for(String fieldName : M.keySet())
    Schema.SObjectField field = M.get(fieldName);
    Schema.DescribeFieldResult F = field.getDescribe();
    //A nillable field can have empty content. A isNillable Boolean non-nillable field must have a value for the object to be //created or saved.
    // if F.isNillable() is false then field is mandatory
    Boolean isFieldreq = F.isNillable()

    Salesforce | Parul Parul #2

    There is a way to programmatically learn about metadata of your datamodel with in Apex.
    Schema Describe calls provides the ability to programitically describe the information about the current org schema such as list of top level objects including custom objects and their fields.

    Map<String,Schema.SobjectType> gd = Schema.getGlobalDescribe();
    We can also use schemaDescribe() to create tree structure of all the objects and fields in the schema browser, to do this we need to use the codemap

    Map<String,Schema.SobjectType> gd = Schema.getGlobalDescribe();

    Returns map of all Sobjects names or Keys to sObject tokens or values for the standard and custom objects defined in the organization.

Viewing 5 posts - 1 through 5 (of 5 total)

Please to reply to this topic.


We're not around right now. But you can send us an email and we'll get back to you, asap.


About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2018 Forcetalks. All Right Reserved.

Log in with your credentials


Forgot your details?