Even though I'm not providing any answers to the original question, I want to add more information to this post that could be useful for others.
Apparently, the nillable
field in the SObject Describe API is not related to the required/optional property of an SObject fields.
Take for instance the description of the ServiceAppointment
object, which specifies the ParentRecordId
as nillable. However, the following code fails:
ServiceAppointment sa = new ServiceAppointment();
insert sa;
The error code I receive when trying to execute the code above is:
System.DmlException: Insert failed. First exception on row 0; first error:
REQUIRED_FIELD_MISSING, Required fields are missing: [ParentRecordId]: [ParentRecordId]
So if we take that error message as truthful, we can succeed:
Account a = new Account(Name='Some Name'); // `Name` is required for `Account`.
insert a;
ServiceAppointment sa = new ServiceAppointment(ParentRecordId=a.Id);
insert sa;
In fact, none of the fields marked as non-nillable are actually required when creating a ServiceAppointment
object. Such fields are the following:
[
"Id",
"OwnerId",
"IsDeleted",
"AppointmentNumber",
"CreatedDate",
"CreatedById",
"LastModifiedDate",
"LastModifiedById",
"SystemModstamp",
"Address",
"EarliestStartTime",
"DueDate",
"IsAnonymousBooking"
]
The reason behind that I think is that all of them are provided with default values when creating a record even though they are not flagged as defaultValue
, defaultValueFormula
nor defaultedOnCreate
(which makes it even more confusing 😠).
I did find out however that the following formula closely approximates the required fields shown in the GUI
function isFieldMandatory(field) {
return (
field.createable &&
!field.defaultedOnCreate &&
!field.nillable
);
}
Such formula will say that the following fields are mandatory, which "matches" what is shown in the GUI (with the exception of the only actual required field, whcih is ParentRecordId
):
[
"EarliestStartTime",
"DueDate"
]
So a message to you, future hackers:
I wish you better luck that what I had so far 😞