2

I am getting all the object and related fields from my org using

public Map schemaMap = Schema.getGlobalDescribe();

Map fieldMap = schemaMap.get(objectName).getDescribe().fields.getMap();

And selecting some fields and saving them into a object and try to display it on a another VF page. But I have issue that when I try to display all the fields it is getting a error on VF page.

Insufficient Privileges You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary.

I don't want to get this error. For that I just only want to display all the object and fields which are accessible to the current login user. Is any one here have any idea how to restrict the object and fields which are not accessible to the user.

1 Answer 1

2

Salesforce provides a mechanism to check whether a particular object or field is accessible to the logged in user or not.

You can do something like below :

Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe(); 
List<String> apiNames = new List<String>();

for(string objectApi: globalDescribe.keyset()){             
    apiNames.add(objectApi);
}

Schema.DescribeSobjectResult[] results  = Schema.describeSObjects(apiNames);
for(Schema.DescribeSobjectResult describeResult  : results){                
    if(describeResult.isAccessible()) {
        System.debug(describeResult.getLabel()+'~~~~~~~~~~~~~~is accessible to this user');
        for(Schema.SobjectField  field: describeResult.fields.getMap().values()){
            if(field.Describe().isAccessible()){
                console.log(field.Describe().getLabel()+'~~~~~~~~~~~~~~~~~~~~is accessible');  
            }
        }
    }
}
0

You must log in to answer this question.

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