Activity Forums Salesforce® Discussions How to know the mandatory fields in standard or custom objects? Reply To: How to know the mandatory fields in standard or custom objects?

  • Ravi

    Member
    July 5, 2016 at 8:40 am

    In Apex you can check this by global describe :

    Schema.DescribeSObjectResult r = User.sObjectType.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() ;
     System.debug ('F = ' + fieldName + ' isnul= ' + isFieldreq);
    }