This logic is being used in a migration from one object (Case) to another object (Care_Recipient_File__c).
There are hundreds of fields on both objects, many of them have the same api names and store the same data points. The ones that differ are handled with a field mapping var, which I've omitted here.
I've verified that when I only copy data from our manually prepared field map, the data migrates succesfully. However, I can run this in exec anon right now and get the same error I got with my batch class:
// Querying all fields from Case
Case c = Database.query('SELECT ' + String.join(new List<String>(Schema.SObjectType.Case.fields.getMap().keySet()), ', ') + ' FROM Case WHERE Id = :sourceCaseId');
// Update fields from provided map
for (String caseField : diffFieldMap.keySet()) {
String crfField = diffFieldMap.get(caseField);
crf.put(crfField, c.get(caseField));
}
// Update fields with matching api names
Map<String, Schema.SObjectField> crfFieldsMap = Schema.SObjectType.Care_Recipient_File__c.fields.getMap();
for (String fieldName : crfFieldsMap.keySet()) {
if (c.getSObjectType().getDescribe().fields.getMap().containsKey(fieldName) && crfFieldsMap.get(fieldName).getDescribe().isUpdateable()) {
crf.put(fieldName, c.get(fieldName));
}
}
When attempting to update said CRF record, I get
Line: 22, Column: 1 System.DmlException: Update failed. First exception on row 0 with id a0ZPb000000CBBkMAO; first error: INSUFFICIENT_ACCESS_OR_READONLY, insufficient access rights on object id: []
I've confirmed that there's no issue with running the dynamic query on the case object that pulls all its fields.
Since I'm checking isUpdateable()
on each matching crf field before attempting to populate, I'm unsure of why I'm getting this access error, since I'm a system admin with full crud and FLS edit permissions.
My best solution so far is to write a batch class just to try and migrate one field at a time between the two records until an offending field is found, but I'm wondering if there's something more obvious that anyone can suggest?