function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mauro_offermannmauro_offermann 

Why FieldSet.getFields() retrieve empty fieldsetMember array?

I created an unmanaged package on  my developer org.

1º I had created a FieldSet on Account Called "AccountFilters" and  added some fields like  "AccountSource".
2º I have the following methods:

public Schema.FieldSet getFieldSet(String fieldSetName) {
  Schema.Describesobjectresult result = Account.getSobjectType().getDescribe();
  Map<String, Schema.FieldSet>  fieldSetMap = result.fieldSets.getMap();
  System.assert(fieldSetMap.size() > 0, 'No hay conjuntos para esta clase');
  Schema.FieldSet fs = fieldSetMap.get(fieldSetName);
  System.assert(fieldSetMap.containsKey(fieldSetName), 'No se encontró el conjunto ' + fieldSetName);
  return fs;
}


public Schema.FieldSetMember[] getFields(String fieldSetName) {
  return getFieldSet(fieldSetName).getFields();
}

The method getFieldSet('AccountFilters') works fine and return the expected fieldset, but the method getFields('AccountFilters') returns an empty FieldSetMember[] array. Why?
Elie.RodrigueElie.Rodrigue
I tried your code in my test org just by copy pasting and it worked fine. Can you post your test code where you are calling getFieldSet and getFields?

The only way i could see an issue is with user permission. To validate that you could add "without sharing" to your class declaration just to make sure this isnt the issue.
mauro_offermannmauro_offermann
Thank you for your answer.
Itried adding 'without sharing' but the problem persist.

This is my test class.

@isTest(SeeAllData=true)
private class ReportTest {
   
static testMethod void getFieldSetTest() {
  Report__c report = new Report__c(Name = 'Test', ColumnFieldSet__c = 'AccountColumns', FilterFieldSet__c = 'AccountFilters', SobjectName__c = 'Account');
  insert report;
ApexPages.Standardcontroller standard = new ApexPages.Standardcontroller(report);
  ReportController controller = new ReportController(standard);
  Test.startTest();
  Schema.FieldSet fieldSet = controller.getFieldSet(report.FilterFieldSet__c);
  Test.stopTest();
  System.assertNotEquals(null, fieldSet, 'El conjunto especificado no existe: ' + report.FilterFieldSet__c);
}
   
static testMethod void getFieldsTest() {
  Report__c report = new Report__c(Name = 'Test', ColumnFieldSet__c = 'AccountColumns', FilterFieldSet__c = 'AccountFilters', SobjectName__c = 'Account');
  insert report;
  ApexPages.Standardcontroller standard = new ApexPages.Standardcontroller(report);
  ReportController controller = new ReportController(standard);
  Test.startTest();
  Schema.FieldSetMember[] members = controller.getFields(report.FilterFieldSet__c);
  Test.stopTest();
  System.assert(members.size() > 0, 'No hay campos en el conjunto especificado: ' + report.FilterFieldSet__c);
}
}



My ReportController is:



public without sharing class ReportController {

public Report__c report {get; set;}
public Schema.Sobjecttype stype {get; set;}

public ReportController(ApexPages.StandardController controller) {
  if (!Test.isRunningTest())
   controller.addFields(new String[] {'Name', 'SOQL__c', 'FilterFieldSet__c', 'ColumnFieldSet__c', 'SobjectName__c'});
  this.report = (Report__c)controller.getRecord();
  Map<String, Schema.Sobjecttype> schemaMap = Schema.getGlobalDescribe();
  this.stype = schemaMap.get(report.SobjectName__c);
}

public Schema.FieldSet getFieldSet(String fieldSetName) {
  Schema.Describesobjectresult result = this.stype.getDescribe();
  Map<String, Schema.FieldSet>  fieldSetMap = result.fieldSets.getMap();
  System.assert(fieldSetMap.size() > 0, 'No hay conjuntos para esta clase');
  Schema.FieldSet fs = fieldSetMap.get(fieldSetName);
  System.assert(fieldSetMap.containsKey(fieldSetName), 'No se encontró el conjunto ' + fieldSetName);
  return fs;
}


public Schema.FieldSetMember[] getFields(String fieldSetName) {
  return getFieldSet(fieldSetName).getFields();
}

}
Kevin Antonioli 6Kevin Antonioli 6
I had this same problem once upon a time. Make sure the fields you want in yuor field set are added to the 'In the Field Set' section and not the 'Available for the Field Set' section. See below screenshot.
User-added image
Moumita Saha 11Moumita Saha 11
Thanks Kevin !