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
SunnySunSunnySun 

Get field type / permissions in the apex code

hi, All

I have a trouble. then i want to get  formula type field .And   I want to know the field for the current user's permissions.

 

Map<String, Schema.SobjectField> fields = 
            Schema.SobjectType.OpportunityLineItem.fields.getMap();

 I can use this find field properties.But I did not find a way to judge whether the formula type field.And  I want to know the field for the current user's permissions too. Used to remove the current user without permissions field.

 

 Any help will be Appreciated.

 

vhanson222vhanson222

To determine if the current user's permissions allow them access to that field, you can use the 'isAccessible' method.  The isAccessible method returns true if the current user can see this field, false otherwise.  The isUpdateable method will return true if the field can be edited by the current user, false 

otherwise.  

 

Also,

you can tell if the field is a formula field by using the 'isCalculated' method, which returns true if the field is a custom formula field, false otherwise. 

 

i.e.:

 

 

            Map<String, Schema.SobjectField> fields = 
                        Schema.SobjectType.OpportunityLineItem.fields.getMap();

            for (string fieldname : fields.KeySet())
            {
                  Schema.Sobjectfield field = fields.get(fieldname);
                  Schema.Describefieldresult fieldDesc = field.getDescribe();
                  Schema.DisplayType fldType = fieldDesc.getType();
                  
                  if (fieldDesc.isAccessible() && fieldDesc.isUpdateable())
                        fieldNames.Add(fieldDesc.getName());

                  System.Debug(fieldDesc.getName() + '   Accessible:' + fieldDesc.isAccessible() + '  Updateable:' + fieldDesc.isUpdateable() + '  Formula:' + fieldDesc.isCalculated());
            }

 

 

I hope that helps.

 

check out this link for more information: Describe Field Result Methods