1.

How To Get All The Required Fields Of Sobject Dynamically?

Answer»

There is no direct property available in Apex dynamic API to represent the required field. However there is another way to know about it.

If any FIELDS have below three properties then it is mandatory field.

  1. If it is Creatable
  2. If it is not nillable and
  3. If it does not have any DEFAULT value

Map<STRING, Schema.SObjectType> m = Schema.getGlobalDescribe() ;

Schema.SObjectType s = m.get(so.apiName) ;

Schema.DescribeSObjectResult r = s.getDescribe() ;

Map<String,Schema.SObjectField> fields = r.fields.getMap() ;

for(String f : fields.keyset())

{

Schema.DescribeFieldResult desribeResult = fields.get(f).getDescribe();

if( desribeResult.isCreateable() && !desribeResult.isNillable() && !desribeResult.isDefaultedOnCreate() )

{

//This is mandatory / required field

}

}

There is no direct property available in Apex dynamic API to represent the required field. However there is another way to know about it.

If any fields have below three properties then it is mandatory field.

Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe() ;

Schema.SObjectType s = m.get(so.apiName) ;

Schema.DescribeSObjectResult r = s.getDescribe() ;

Map<String,Schema.SObjectField> fields = r.fields.getMap() ;

for(String f : fields.keyset())

{

Schema.DescribeFieldResult desribeResult = fields.get(f).getDescribe();

if( desribeResult.isCreateable() && !desribeResult.isNillable() && !desribeResult.isDefaultedOnCreate() )

{

//This is mandatory / required field

}

}



Discussion

No Comment Found