0

I am trying to populate a Set with values dynamically generated from a given object.

SObjectType objectName = Schema.getGlobalDescribe().get('Account');
Map<String,Schema.SObjectField> fields = objectName.getDescribe().fields.getMap();
Set<String> accountFields = new Set<String>{};
for(Schema.SObjectField fieldApiName: fields.values()) {
    accountFields.add(fieldApiName);
    system.debug(fieldApiName);
}

Using this code however returns an incorrect signature error:

Method does not exist or incorrect signature: void add(Schema.SObjectField) from the type Set

1 Answer 1

3

An SObjectField is not a String. You need to either change the Set type, or convert to String values:

accountFields.add(String.valueOf(fieldApiName));

... or ...


Set<SObjectField> accountFields = new Set<SObjectField>();

Ultimately, it depends on your intent. If you just need to know which fields exist, the SObjectField tokens require less memory, but if you want to ultimately generate a query, the String values are required.

You must log in to answer this question.

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