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?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this community@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.
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]