5

I'm trying to find required fields for lead object, I want to make sure this correct logic, because its giving field results which are not marked as required.I'm using isNillable() method in field describe to find out the result, below is the sample code.

Map<String, Schema.SObjectField> fieldsMap = Schema.SObjectType.Lead.fields.getMap();
for(String key : fieldsMap.keySet()){
    Schema.DescribeFieldResult result = fieldsMap.get(key).getDescribe();
    if(!result.isNillable() && result.isCreateable()) {
        System.debug(logginglevel.info, '+++++++++ result = '+result.getName());

    }                   
}
  • 2
    if added !isDefaultedOnCreate() in if condition, it seems to be working. – realnumber Jun 19 '14 at 0:05
  • If that's the case you should post that as an answer to your own question, then you can mark it accepted once enough time has expired (couple of days). – Matt Lacey Jun 19 '14 at 3:32
5

To identify required fields !isDefaultedOnCreate() is required in addition to !result.isNillable() && result.isCreateable().

Map fieldsMap = Schema.SObjectType.Lead.fields.getMap();

for(String key : fieldsMap.keySet())
{
    Schema.DescribeFieldResult result = fieldsMap.get(key).getDescribe();
    if(!result.isNillable() && result.isCreateable() && !result.isDefaultedOnCreate()) 
    {
        System.debug(logginglevel.info, '+++++++++ result = '+result.getName());
    }                   
}

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.