We are updating our code to use the new accessLevel
parameter in the update
method of the Database
class. We have cases where the object contains fields that cannot be edited (e.g. LastModifiedDate
).
If we update the object using the normal update
, the update works because we have not actually updated LastModifiedDate
. However, if we use Database.update
with AccessLevel.USER_MODE
, we see the exception:
System.DmlException: Operation failed due to fields being inaccessible on Sobject Account, check errors on Exception or Result!
Sample code we have used is as below. The debug message prints:
The field(s) "LastModifiedDate" are not accessible for update of Account
Account acc = new Account(name='Acc1');
insert acc;
acc = [select id, name, lastModifiedDate from Account where id = :acc.id];
acc.name = 'Acc1_updated';
update acc;
try {
Database.update(acc, AccessLevel.USER_MODE);
} catch (DMLException dex) {
System.debug('The field(s) "' +
String.join(dex.getDmlFieldNames(0), ', ') + '" are not accessible for update of Account');
throw dex;
}
Similar issue with auto numbered name fields, formula fields, master-detail lookup fields etc.
Is there any way to use Database.update
to update the object other than removing the field LastModifiedDate
from the SOQL query select list? Any pointers will be appreciated.
LastModifiedDate
in the query? The code you have provided is not using it