function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PGX64PGX64 

Get Required fields from a describeSObjectResult call?

Hello,

 

Maybe I missed something, but I'm having trouble finding which fields are required based on the results of a describeSObjectResult.  I've tried searching through the documentation and other postings, but did not come up with much information.

 

What I'm trying to do is loop through the list of fields for a particular object (chosen at run-time) and mark each required field.  Unfortunately, it doesn't appear that the the Field objects found in the DescribeSObjectResult object have an "isRequired" method to easily tell.  I tried to cobble something together, assuming that if isNillable and isDefaultedOnCreate were both false, then it was required, but this doesn't appear to be completely accurate.

 

I would appreciate any help.  Perhaps I'm missing something or there is another approach that I can take. Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell
Requiredness is largely a feature of the page layout, not the data model, so you'll need to look at the relevant describeLayout results (assuming it is the UI requiredness you're trying to replicate)

All Answers

SuperfellSuperfell
Requiredness is largely a feature of the page layout, not the data model, so you'll need to look at the relevant describeLayout results (assuming it is the UI requiredness you're trying to replicate)
This was selected as the best answer
PGX64PGX64
Thanks!  I got an example working with the method you suggested.  Basically what I'm looking for is a reliable method to find which fields are absolutely required to create or update an object via the API.  I have a separate application that maps local data to Salesforce object fields, and I needed a way to mark the cells of data I have as required.  I'm not so much trying to replicate the internal Salesforce interface as just provide an easy way to populate the Salesforce objects.  So, the idea that I was going with was to test if a given field is createable or updateable first (and if so, add it to the data collection) and then if it's not nillable and not default on create then it was required.  Is this a valid algorithm?  I was hoping for a cleaner way to determine requiredness as I seemed to be having problems with a few objects - I will have to double-check which those were now.
FaridKognozFaridKognoz

I believe you're on the right path. Just one thought, is really a must for the field to be updateble in order to be required?

I think a field is required if and only if:

 

IS Creatable

 

IS NOT Nillable

 

IS NOT Defaulted on Create