3

I know that the name of the object is Obje and the name of the field set for this object if Fieldse.

Now I would like to get a List<Schema.SObjectField> (i.e. fields from the field set).

Right now I found only this approach:

public static List<Schema.FieldSetMember> readFieldSet(String fieldSetName, String ObjectName) {
        Map<String, Schema.SObjectType> GlobalDescribeMap = Schema.getGlobalDescribe();
        Schema.SObjectType SObjectTypeObj = GlobalDescribeMap.get(ObjectName);
        Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe();

        Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(fieldSetName);

        return fieldSetObj.getFields();
}

But here I am getting List<Schema.FieldSetMember> instead of List<Schema.SObjectField>.

Thank you.

After trying out to apply the answer to my problem I ended up with the following code:

public static List<Schema.SObjectField> readFieldSet(String fieldSetName, String objectName) {
        List<Schema.SObjectField> res = new List<Schema.SObjectField>();

        Map<String, Schema.SObjectType> GlobalDescribeMap = Schema.getGlobalDescribe();
        Schema.SObjectType SObjectTypeObj = GlobalDescribeMap.get(objectName);
        Schema.DescribeSObjectResult describeSObjectResultObj = SObjectTypeObj.getDescribe();
        Schema.FieldSet fieldSetObj = describeSObjectResultObj.fieldSets.getMap().get(fieldSetName);
        for (Schema.FieldSetMember f: fieldSetObj.getFields()) {
            res.add(describeSObjectResultObj.fields.getMap().get(fieldSetMember.getFieldPath()).getSObjectField());
        }

        return res;
    }

And I am getting an error

Cannot resolve symbol getSObjectField

enter image description here

What am I missing here?

3

List<Schema.FieldSetMember> is just what Schema.FieldSet.getFields() gives you. You can call getFieldPath() on each field set member to obtain the field's API name, and then convert to a Schema.SObjectField via

Schema.SObjectType.My_Object__c.fields.getMap().get(fieldSetMember.getFieldPath());

So given your existing code, you can do something like

Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(fieldSetName);
List<Schema.SObjectField> fieldList = new List<Schema.SObjectField>();

for (Schema.FieldSetMember f: fieldSetObj.getFields()) {
    fieldList.add(res.fields.getMap().get(f.getFieldPath()));
}

return fieldList;

This might not be the most efficient way to handle this information, but the best approach will depend on your ultimate use case for the field set details.

  • I updated my question. In case you will have some time, help me out, please. Thank you. – yourbuddy Nov 20 '18 at 17:30
  • @yourbuddy The mistake was mine; apologies. I think the updated details above are correct. – David Reed Nov 20 '18 at 17:52

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.