You need to sign in to do that
Don't have an account?
RedSales
How to find Required Fields For An Object?
Hello,
I've been trying to find out the required fields for some objects but was not sure how I could do this. For example the User object has details specified as follows http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_user.htm#topic-title How can I find out from this though which fields are actually required fields when creating a new user & which are not?
Thanking you in advance for your help!
Use this code
All Answers
For starters, just worth mentioning that some fields might be required at the object level and some others on the page layout level. If your interaction is limited to via the API, then the Page Layout mandatory fields dont apply.
Didn't think it was possible to programatically ascertain mandatory field for an object. Just having a look at the Describe Field Result, and there doesnt seem to be a isMandatory or isRequired method
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_fields_describe.htm
Use this code
Thank you Ritesh, it is the object level I am interested in & not the page layout level. I presume there must be some way of finding this out alright. Thank you again for your help. I guess somebody might know if this is possible. It will be interesting to find out.
Thanks again.
Thanks Shashikant, I will try this out.
Thanks again folks for the help,
The following based on Shashikant's suggestion works.
So basically Shashikant's message should be marked as solution, I am doing same hope you won't mind.
That's fine Ankit, I was thinking that Shashikant's message should be the solution but as I modified it slightly I just thought I'd highlight the change that worked to others viewing the post. Apologies Shashikant's & thanks for your help. I think there should be an option added to the forum to accept more than one response as a solution.
Thanks all for the help.
I agree with you that there should be a option to mark more than 1 solution as one is for the original contributor and other is for the final answer. Hope admins will consider this.
<apex:repeat value="{!fields}" var="f">
<apex:inputField value="{!merch[f.fieldPath]}"
required="{!OR(f.required, f.dbrequired)}"/>
</apex:repeat>
</apex:pageBlockSection>
Developer docs: http://www.salesforce.com/us/developer/docs/pages/Content/pages_dynamic_vf_field_sets.htm
Below is a modified version which gives us the Field Labels and APIs in a key-value format.
Schema.DescribeSObjectResult r = Account.sObjectType.getDescribe();
Map<String,Schema.SObjectField> M = r.fields.getMap();
for(String fieldName : M.keySet()){
Schema.SObjectField field = M.get(fieldName);
Schema.DescribeFieldResult F = field.getDescribe();
//A nillable field can have empty content. A isNillable Boolean non-nillable field must have a value for the object to be
//created or saved.
// if F.isNillable() is false then field is mandatory
Boolean isFieldreq = F.isNillable() ;
if(isFieldreq == false){
//System.debug ('F = ' + field + ' isnul= ' + isFieldreq);
System.debug (F.getLabel() +': ' + field );
}
}
Output:
Account=(Id, IsDeleted, Name, OwnerId, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById, SystemModstamp,...),
Contact=(Id, IsDeleted, LastName, Name, OwnerId, DoNotCall, CreatedDate, CreatedById, ...)
But I also wonder why Salesforce don't make this info visible in the Object Manager --> Field and Relationships screen
Take for instance the ServiceAppointment object. As per its description metadata, the fields EarliestStartTime and DueDate are required (i.e. they are marked as non-nillable, and also as creatable), and the field ParentRecordId it not required (i.e. it is marked as nillable).
However, the field EarliestStartTime and DueDate are in fact not required to create a ServiceAppointment object, and the field ParentRecordId is in fact required.
This code works:
Account a = new Account(Name='Some Name');
insert a;
ServiceAppointment sa = new ServiceAppointment(ParentRecordId=a.Id);
insert sa;
This code DOESN'T work:
ServiceAppointment sa = new ServiceAppointment(
EarliestStartTime=Date.parse('2020-11-22'),
DueDate=Date.parse('2020-11-22')
);
insert sa;
At this moment I'm unable to accurately determine which fields are required for an object. So far, I thought the following formula would work but I've manually verified that IT DOESN'T:
```
function isFieldMandatory(field) {
return (
field.createable &&
!field.defaultedOnCreate &&
!field.nillable
);
}
```
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ParentRecordId]: [ParentRecordId]