I'm designing a VF SPA that uses remote objects. Much of the page is dynamic, and I use user input to define which sObjects I look up. I wanted to know how, given a user input for a sObj, I can return the fields on that object as well as which are "nillable"/required.

A simple example of this would be a page that allows a user to create a new sObj. The user can select the type of sObj they would like to create, i.e. Contact, and the page would render all of the required fields on that sObj.

Can this be done solely via remote objects?

If not, perhaps the way to go is to use remoting and pass an apex method an object type and have the method return an array of fields that are required? In that case, how do I use javascript remote objects where you have to define which fields you want to pull from each object ()? Is it possible to just pull all fields and then, depending on the results from the remote apex class, use only a subset of them?

Thank you!

  • RemoteAction and describes seem like a good approach. You will need some manual maintenance though as fields can become "required" through validation rules or triggers and you can't really interpret that programmatically. – Adrian Larson Dec 12 '15 at 20:22
  • Let me make sure I've understood you. Above I am defining "Required" by isNillable(). But there are other elements, such as triggers and validation rules, which can make a field "required" without changing it's isNillable(). Is that correct? – ZAR Dec 12 '15 at 20:37
  • You've got it exactly. So you would need to maintain a hard coded map of SObjectType to List <SObjectField> for those values. You could augment that collection with describes so you don't need to do quite as much work. – Adrian Larson Dec 12 '15 at 20:42
  • Can't answer from my phone with how I would do it but I'll try to when I get in front of a computer. – Adrian Larson Dec 12 '15 at 20:43
  • Thanks! Considering that I have to keep a hardcoded version, I wonder if it wouldn't be more simple to just keep some JSON that describes the fields required and I manually edit that. That way any time I need to add/remove a required field, it's in one JSON. Could be a good resource to keep. – ZAR Dec 12 '15 at 20:48

I approach a similar problem in my SObjectFactory test utility with a RequiredFieldsCache, but you can adopt a somewhat simpler approach because you don't care about the values for a particular field.

public class NonNillableFields
    public List<SObjectField> get(SObjectType sObjectType)
        return cache.containsKey(sObjectType) ?
            cache.get(sObjectType) : new List<SObjectField>();
    public List<String> getFieldPaths(SObjectType sObjectType)
        List<String> fields = new List<String>();
        for (SObjectField field : get(sObjectType)
        return fields;
    static Map<SObjectType, List<SObjectField>> cache = 
        new Map<SObjectType, List<SObjectField>>
        Account.sObjectType => new List<SObjectField>
            Account.Name, Account.ContactId // etc.

Then you can use this cache in a variety of ways, but particularly in your controller/extension.

public with sharing class MyExtension
    public static List<String> getRequiredFields(String requestedType)
        SObjectType sObjectType = Schema.getGlobalDescribe().get(requestedType);
        return NonNillableFields.getFieldPaths(sObjectType);

I think you need to use List<String> because JSON.serialize won't work on SObjectField.

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.