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

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.

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.