0

I know the question has already been posted, and answers where given ; I read this topic ; Of course I can try to insert the sObject and get the error listing the missing fields,

But isn't it a 'better' way to do it ?

I'm pretty sure I had found a way, but can't find it anymore ;

At this moment my favorite option remains to use the Schema Builder in wich it's obvious (red lines in front of each required attribute) -->

enter image description here

But isn't it a way to show a column required / not required, in the object manager ? -->

enter image description here

Same question, in the dev console ? -->

enter image description here

Thanks for your help !

  • Looks like a candidate for idea exchange. – Pranay Jaiswal Sep 7 '18 at 13:30
  • How do you do ? How do people do, most of the time ? Thanks ! – Alexis MASSON Sep 7 '18 at 13:38
  • 2
    I use IDE like eclipse/VS code to retreive metadata of Object. The metadata XML contains a required tag which tells me if its a required field of not. Other way is looking at UI/Describe call like you mentioned. – Pranay Jaiswal Sep 7 '18 at 13:40
4

Please note that fields can be made required via other means which are difficult or impossible to detect. Validation rules, triggers, and Process Builder can all introduce errors when a field is not populated (sometimes more intentionally than others).

Given the above, you can still iterate the describe to find which fields are practically required if you like. Here is a script showing how to do so.

for (SObjectField field : SObjectType.MyObject__c.fields.getMap().values())
{
    if (!field.getDescribe().isNillable())
    {
        system.debug(field);
    }
}
  • Thanks for this pointing this issue, and thanks for the code ! Ok, I'll assume my idea wasn't as good as it first seem to me, thanks for your usefull help ! – Alexis MASSON Sep 7 '18 at 14:39
2

For API version <= 44.0, there is no explicit isRequired method but there are a few methods that you can leverage to find required fields:

for (Schema.SObjectField fieldToken : Schema.SObjectType.MyObject__c.fields.getMap().values())
{
    Schema.DescribeFieldResult fieldDescribe = fieldToken.getDescribe();
    if (
        // These methods are on DescribeFieldResult
        !fieldDescribe.isNillable()
        && fieldDescribe.isCustom()
        && !fieldDescribe.isDefaultedOnCreate() // optional
    )
    {
        system.debug(field);
    }
}

Schema.DescribeFieldResult Documentation

!isNillable() won't exactly get you your required fields since this will also catch auto-numbers and system fields (like Deleted).

isCustom() I believe is an auto-inclusion since this type of use case typically relates to custom objects and using auto-numbering for a Name is pretty common in my experience. Exclude this is you are leveraging the standard required fields for record creation.

!isDefaultedOnCreate() may be good to add depending on whether or not you care about values being explicitly declared that would otherwise default.

0

Found this, Could help also !

Not responding at my whole question, but helps.

enter image description here

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.