2

I'm working on testing whether or not certain fields are accessible with FLS. At the moment, using the following method, I can only test one field at a time.

I'm fairly new to APEX, and I'm wondering can I test multiple fields somehow? Possibly loop through a list of fields I could pass into the FieldInformation method?

APEX CONTROLLER:

public String FieldInformation() {

    if (!Schema.sObjectType.Opportunity.fields.Amount.isAccessible()){
      System.debug('IS NOT ACCESSIBLE');
      return '';
    }

    Schema.DescribeFieldResult drField = Opportunity.Amount.getDescribe();
    System.debug('GET LABEL: ' +  drField.getLabel());
    System.debug('GET PICKLIST VALS: ' + drField.getPicklistValues());
    System.debug('GET IS ACCESSIBLE: ' + drField.isAccessible());
    System.debug('GET IS CREATABLE: ' + drField.isCreateable());
    System.debug('GET IS NILLABLE: ' + drField.isNillable());
    System.debug('GET IS UPDATEABLE: ' + drField.isUpdateable());       
    return '';
}

UNIT TEST:

@isTest
static void getOpp() {

    Opportunity opportunity = new Opportunity(name = 'Test opportunity', StageName='Prospecting', CloseDate=Date.today());

    insert opportunity;

    ApexPages.currentPage().getParameters().put('OppId', opportunity.Id);

    temp_FLS FLSController = new temp_FLS();

    FLSController = new temp_FLS(); 

    FLSController.FieldInformation(); 

}

1 Answer 1

3

The describe results for the SObjectType include a map of field names to SObjectField tokens. So for example this will output the field describe information for all fields:

Map<String, SObjectField> m = Opportunity.SObjectType.getDescribe().fields.getMap();
for (String name : m.keySet()) {
    DescribeFieldResult r = m.get(name).getDescribe();
    System.debug(r);
}

and as you suggest you could iterate over a list of field names passed into your method instead of all the keys.

(There is no API to get multiple DescribeFieldResult objects returned in one call.)

2
  • Thanks for the informative answer Keith, really appreciate it! Sorry to ask a further question, but would passing a list of fields into this method be a complicated thing to do? As I mentioned above, I'm fairly new to Apex and Java.
    – Daft
    Jun 16, 2014 at 11:23
  • 1
    No its easy as you can make your function public String FieldInformation(String[] names) and then loop over the names.
    – Keith C
    Jun 16, 2014 at 12:37

You must log in to answer this question.

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