You need to sign in to do that
Don't have an account?
mauro_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?
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?
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.
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();
}
}