0

I have a list of sObjects. In this list I have records of Account,Contact and Opportunity which needs to be updated. so before updating the list I am doing CRUD check on Account , Contact and Opportunity like below:

List<sObject> lstOfSObjects = new List<sObject>();
lstOfSObjects.add(new Account(.......));
...
lstOfSObjects.add(new Contact(.......));
...
lstOfSObjects.add(new Opportunity(.......));
...
if (!Schema.sObjectType.Account.isUpdateable()) {
 return...                   
}
if (!Schema.sObjectType.Contact.isUpdateable()) {
 return...                   
}
if (!Schema.sObjectType.Opportunity.isUpdateable()) {
  return...                
}

update lstOfSObjects;

Even if I am checking CRUD check for every sObject still Apex PMD is throwing an error at the update DML saying : Validate CRUD before DML Operation.

Any help?

2
  • These answers should help you out.
    – TSmith
    Commented Sep 4, 2020 at 12:32
  • 2
    I think it's because you're using generic SObject list and not specific SObject
    – ytiq
    Commented Sep 4, 2020 at 12:54

1 Answer 1

1

PMD either has a problem with a list of generic SObjects or it doesn't like the conditional blocks with early return statements.

This format may pass:

List<sObject> lstOfSObjects = new List<sObject>();
lstOfSObjects.add(new Account(.......));
...
lstOfSObjects.add(new Contact(.......));
...
lstOfSObjects.add(new Opportunity(.......));
...

if (Schema.sObjectType.Account.isUpdateable() && Schema.sObjectType.Contact.isUpdateable() && Schema.sObjectType.Opportunity.isUpdateable()) {
    update lstOfSObjects;
}

I know DML limits are a real concern, so if wrapping the DML call in an if-block still doesn't satisfy PMD you could also try making three lists of Accounts, Contacts, and Opportunities, and then finally use List<SObject>.addAll() to combine them together for the update.

1
  • Yea, the example that you shared, it worked.. Thank you so much..!!
    – sfdc07
    Commented Sep 4, 2020 at 19:34

You must log in to answer this question.

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