Tuesday 19 November 2013

How to find the required fields of an object in SFDC


 I was trying to find out how to get the list of required fields of an objects in SFDC and finally got this code. 
 
1.   Open Execute Anonymous Window in developer console
           Debug --> Open Execute Anonymous Window.

2.  Copy and Paste the below code and click on execute. I am using the User Object in the code. Replace with the Object for which you want to find the required fields
            Note : Check on Open Log checkbox.

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())
            System.debug (' F = ' + fieldName + '*****    isnul= ' +  F.isNillable());         
      }


Below is the log file generated once you execute above code block. Check on debug only filter to only see debug logs. 










Thanks to  Shashikant Sharma for the solution.


source:  Force.com Discussion Boards

No comments:

Post a Comment