1

Just posting this because it took me a bit of time to get my answer. For anyone wanting to get a list of all the Fields present on all the FieldSets of an Object, here is my method.

Hope it will be usefull for someone day.

1 Answer 1

1
private String getUsedFields() {
    Set<String> usedFields = new Set<String>();
    for (String fieldSet : Schema.SObjectType.'SObject'.fieldSets.getMap().keySet()) {
        for (Schema.FieldSetMember field : Schema.SObjectType.'SObject'.fieldSets.getMap().get(fieldSet).getFields()) {
            usedFields.add(field.getFieldPath());
        }
    }
    
    List<String> fieldList = new List<String>(usedFields);
    String fields = String.join(fieldList, ', ');
    return fields;
}

Replace 'SObject in my code by the APi name of your SObject.

If you want to have duplicate, use a List instead of a set.

the String.Join() at the end is useful if you want to transform your list into a string with your values sepparated by commas to use in a Database.query like me. and you must transform your Set into a List if you want to do it.

1
  • By the way, the last three lines can actually be written as: return String.join((Iterable<String>)fieldList,', '); For whatever reason, while you can't pass a Set to String.join directly, casting it is totally allowed.
    – sfdcfox
    Commented Jun 14, 2022 at 0:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .