I have ran the Apex PMD, In my result in got the security error with the rule ApexCRUDViolation

so I'm checking the field-level read permission for the selected field before querying as below.

            Enrollment__c.SObjectType.getDescribe().isAccessible()  &&
            Schema.SObjectType.Enrollment__c.fields.Id.isAccessible()  && 
            enrollstatus =  [select  Id, Enrollment__c, (select Id from family__r), 
                              from Enrollment__c WHERE Id=:varid];          

but still I'm getting ApexCRUDViolation for this SOQL, Do we need to add for Child record too?

I tried by adding like Schema.SObjectType.Enrollment__c.fields.family__r.Id.isAccessible(), I'm getting error, Can any one please help me on this.

  • I think the use of stripInaccessible would make your code way easier. May 22 '20 at 6:35
  • You add the check in the same way you have for the parent's object type and fields, so add direct checks you have access to the child object type and the child object type's Id.
    – Phil W
    May 22 '20 at 6:56
  • @phil , Means one More If Condition May 22 '20 at 7:08

The schema doesn't support access via the relationship field to permissions. Instead you must directly check the related object's permissions. On the assumption that "Family__r" is a relationship to a custom object called "Family__c" you can check the permissions like:

            Enrollment__c.SObjectType.getDescribe().isAccessible()  &&
            Schema.SObjectType.Enrollment__c.fields.Id.isAccessible()  && 
            Schema.SObjectType.Enrollment__c.fields.Enrollment_Status__c.isAccessible() &&
            Schema.SObjectType.Family__c.isAccessible() &&

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.