Skip to content

[apex] ApexCRUDViolation: False positive with Lists of Objects with getSObjectType().getDescribe() #3877

Closed

Description

@aaronmorris

Affects PMD Version:
6.42.0+
7.0.0

Rule: ApexCRUDViolation

Description:
I'm getting the "Validate CRUD permission before SOQL/DML operation" error when updating a list of a custom object, but not on the individual.

Code Sample demonstrating the issue:

public static CustomObject__c updateCustomObject(CustomObject__c customObject) {
    if (CustomObject__c.getSObjectType().getDescribe().isUpdateable() == false) {
        throw new DmlException('No permissions to update.');
    }

    // This is fine with no PMD Error
    update resultObject;
    return resultObject;
}

public static List<CustomObject__c> updateCustomObjects(List<CustomObject__c> customObjects) {
    if (CustomObject__c.getSObjectType().getDescribe().isUpdateable() == false) {
        throw new DmlException('No permissions to update.');
    }

    // Getting PMD Error: Validate CRUD permission before SOQL/DML operation
    update resultObjects;
    return resultObjects;
}

Expected outcome:
PMD reports a violation at line ..., but that's wrong. That's a false positive.
Since I've checked the object is Updateable, shouldn't that apply to individuals and lists

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]
ApexPMD Extension in VSCode - chuckjonas.apex-pmd (https://marketplace.visualstudio.com/items?itemName=chuckjonas.apex-pmd)

Activity

  1. added
    a:false-positive
    on Mar 28, 2022
  2. added
    good first issue
    on Apr 29, 2022
  3. sreenath-tm commented on Sep 4, 2022

    @sreenath-tm

    I'm interested in contributing to this issue, so before I start working it, would you mind sparing your time explaining what the fix will be like and pointing me to some resources to get started.

  4. lfalcantar commented on Sep 29, 2022

    @lfalcantar
    Contributor

    @sreenath-tm are you still working on this, otherwise I will like to take it.

  5. sternlexa commented on Nov 1, 2022

    @sternlexa

    I have exactly the same problem with custom objects. Has anyone found a solution?

  6. adangel commented on Nov 3, 2022

    @adangel
    Member

    I had a look at the problem and I can't directly reproduce it... without a SOQL expression, the rule is not triggered at all.

    So, my sample code looks like this:

    public class Foo {
        public static List<CustomObject__c> updateCustomObjects() {
            List<CustomObject__c> customObjects = [SELECT CustomObject__c FROM CustomObject WITH SECURITY_ENFORCED];
            if (CustomObject__c.SObjectType.getDescribe().isUpdateable() == false) {
                throw new DmlException('No permissions to update.');
            }
    
            // Getting PMD Error: Validate CRUD permission before SOQL/DML operation
            update customObjects;
            return customObjects;
        }
    }

    With that, it works.

    Note, the difference in accessing the SObjectType:

    CustomObject__c.SObjectType.getDescribe().isUpdateable()
    

    vs.

    CustomObject__c.getSObjectType().getDescribe().isUpdateable()
    

    Are these two equivalent? If yes, then the problem is, that the rule doesn't support the getter, it only supports the property SObjectType...

  7. SurinderjitBajwa commented on Sep 11, 2023

    @SurinderjitBajwa

    CWS_AzureAccessToken__c existingToken = [Select ID, Name, CWS_Access_Token__c, CWS_Expire_At__c From CWS_AzureAccessToken__c WITH SECURITY_ENFORCED Limit 1];
    DateTime currentDateTime = DateTime.now();
    if(existingToken != null){
    if(Schema.SObjectType.CWS_AzureAccessToken__c.IsUpdateable() == true
    && Schema.SObjectType.CWS_AzureAccessToken__c.fields.CWS_Access_Token__c.IsUpdateable() == true
    && Schema.SObjectType.CWS_AzureAccessToken__c.fields.CWS_Expire_At__c.IsUpdateable() == true ){
    existingToken.CWS_Access_Token__c = newAccessToken;
    existingToken.CWS_Expire_At__c = currentDateTime.addSeconds(3000);
    Update existingToken;
    // Still getting PMD error on this line.
    }else{
    throwDMLException();
    }
    }
    image

  8. added
    needs:pmd7-revalidation
    on Mar 17, 2024
  9. removed
    needs:pmd7-revalidation
    on Apr 2, 2024
  10. AngeloChispa commented on May 12, 2026

    @AngeloChispa

    Hi, is anyone still working on this ?, if not, i would like to take it.

  11. changed the title [-][apex] ApexCRUDViolation on Lists of Objects[/-] [+][apex] ApexCRUDViolation: False positive with Lists of Objects[/+] on Jun 11, 2026
  12. changed the title [-][apex] ApexCRUDViolation: False positive with Lists of Objects[/-] [+][apex] ApexCRUDViolation: False positive with Lists of Objects with getSObjectType().getDescribe()[/+] on Jun 11, 2026
  13. added this to the 7.26.0 milestone on Jun 11, 2026
  14. added 3 commits that reference this issue on Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivegood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions