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
James Henderson 11James Henderson 11 

field set member accessibility

I am attempting to use Field Sets in my Apex code to dynamically query for an object. How can I check the field accessibility of the field members that are not part of the object.

For example, for an Account field set I can easily check the accessibilty of the fields Account Name, Account Number etc.
But I am unable to check the accessibilty for example Parent Account ID > Account and Record Type ID > Name

Ajay K DubediAjay K Dubedi
Hi James

You can search:-> sObjectName Soap Api in sfdc.

Refer This URL:
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_account.htm

You can also add extensions which is name given below.

"Salesforce API Fieldnames"

Please let me know if you have any query.
Please mark it as best Answer if you find it helpful.

Thank You
Ajay Dubedi
James Henderson 11James Henderson 11

Hi Ajay,

This doesn't answer my question about accessibility of field set fields using APEX, your link takes me to the SOAP API.

I have currently create the following function to check the accessibility of field set values, but it will easily fail if I have missed a field relationship or identified it incorrectly. It feels a really hacky way of doing it.

private static string createFieldSetQueryString(Schema.FieldSet fieldSet){
        
        // Get the type of object the field set is a member of
        Schema.SObjectType type = fieldSet.getSObjectType();
        
        // Check to see if the object is accessible
        if(!type.getDescribe().isAccessible()) {
            return null;
        }
        
        // Get list of field set members
        List<Schema.FieldSetMember> fieldSetMembers = fieldSet.getFields();
        
        // Create sting to contain accessible fields in the correct format to add to a query as select fields
        string selectColumn = null;
        
        // Count of number of fields selected
        Integer count = 0;
        for (Schema.FieldSetMember f : fieldSetMembers) {
            try{
                
                Map<String,Schema.SObjectField> m = schema.getGlobalDescribe().get(type.getDescribe().getName()).getDescribe().fields.getMap();  
                string fieldPath = f.getFieldPath();
                string fieldToCheck = fieldPath;
                // If there is a relationship field set member the field path with be of the format "object.field"
                String[] fieldDetails = fieldPath.split('\\.');
                
                // If there are two fieldDetails contained in the list this means that the field member is related to a different object
                if(fieldDetails.size() == 2 && (fieldDetails[0] == 'CreatedBy' || fieldDetails[0] == 'LastModifiedBy' || fieldDetails[0] == 'Owner')){
                    // CreatedBy, LastModifiedBy and Owner are all User objects
                    m = schema.getGlobalDescribe().get('user').getDescribe().fields.getMap();
                    fieldToCheck = fieldDetails[1];
                } else if(fieldDetails.size() == 2 && (fieldDetails[0] == 'Parent' || fieldDetails[0] == 'MasterRecord' || fieldDetails[0] == 'ReportsTo')){
                    // Parent, MasterRecord and ReportsTo are all instances of the same object
                    fieldToCheck = fieldDetails[1];
                }else if(fieldDetails.size() == 2){
                    // All other fields relate to an object using the fieldDetails[0] as the object type
                    m = schema.getGlobalDescribe().get(fieldDetails[0]).getDescribe().fields.getMap();
                    fieldToCheck = fieldDetails[1];
                }
                
                // Check if the user has access on the each field            
                if (m.get(fieldToCheck).getDescribe().isAccessible()) {
                    if(count == 0)
                    {
                        selectColumn = fieldPath;
                    }
                    else
                    {
                        selectColumn = selectColumn + ', ' + fieldPath; 
                    }
                    count++;
                }           
            }   
            catch(Exception ex){
                // Catch expection to ensure that if the check fails then we still return the valid fields.
                system.debug('Accessible check failed for Field Set Member: ' + f.getFieldPath());
            }
        }
        return selectColumn;
    }