3

I want to know how to find Required Fields For An Object. Either through code or through a GUI.

If I try to create a new User, I can't because some fields are required to be defined. How can I tell which one of those are required?

2

Option 1: Found this code which returns a list of all columns and which ones are nillable and createable.

@isTest
public with sharing class Test_Foobar {
  static testMethod void testFoobar(){

    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();
         Boolean isFieldreq  = F.isNillable() ;

         System.debug ('F = ' + fieldName + ' isnillable = ' +  isFieldreq);
         Utils.log('F = ' + fieldName + ' isnillable = ' +  isFieldreq);
    }
  }
}

Prints a mountain of output, which I've condensed here:

F = street isnillable = true
F = street isCreateable = true
F = userpreferencesdisablebookmarkemail isnillable = false
F = userpreferencesdisablebookmarkemail isCreateable = true
...
F = fullphotourl isnillable = true
F = fullphotourl isCreateable = false

Which fields are required?

If a field is nillable then it can have no value. If a field is not nillable, it must have a value. If a field is createable then you can define it, otherwise you can't define it.

So required fields you have to define are creatable and not nillable. Bazinga.

Option 2: Attempt the insert with one field, read the Required fields are missing message:

Inside of a method, do this:

User testUser = new User(firstname = 'joe', lastname = 'bieber');
insert testUser;

You'll get an error message:

15:26:29.082 (3082662769)|VF_PAGE_MESSAGE|Required fields are missing: [Username, 
Email, Alias, CommunityNickname, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, 
ProfileId, LanguageLocaleKey]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.