0

This is my code :

public static List <String> getFields (Id IdRecord) {
 SObjectType  objType = ((SObject) (Type.forName('Schema.' + IdRecord).newInstance())).getSObjectType();
  Map<String, Schema.SObjectField> schemaFieldMap = objType.getDescribe().fields.getMap();
  List<String> Fields = new List<String>();
  
    for (SObjectField field : schemaFieldMap.values()){
        
        Fields.add(Field.getDescribe().getName());
        
    }
    system.debug('fields=' +Fields);
    return Fields;
}

1 Answer 1

1

To return more than one value, you need a "wrapper":

public class sObjectDescribe {
  public String sObjectName;
  public String[] fields = new String[0];
}
public static sObjectDescribe getFields(Id recordId) {
  sObjectDescribe result = new sObjectDescribe();
  result.sObjectName = recordId.getSObjectType()+'';
  for(sObjectField field: recordId.getSObjectType().getDescribe().fields.getMap().values()) {
    result.fields.add(field+'');
  }
  return result;
}

Notice how we can use string concatenation to convert the object and field names without getting a describe. Also see how we can get the sObjectType directly from the Id, you don't need to call Type.forName or anything here.

You must log in to answer this question.

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