Reputation: 445
Even though I have a System Administrator profile assigned to me, and have the CRUD permissions on the Order Line Item Object, I am receiving the below error when I try to delete the Order Item records through DML Operation in my apex class.
System.DmlException: Delete failed. First exception on row 0 with id 8022M00000ANsKKQA1; first error: INSUFFICIENT_ACCESS_OR_READONLY, insufficient access rights on object id: []
I have written the Apex Class as "Without sharing" to avoid any conflict related to sharing rules. But still, I am receiving this error. Can anyone please let me know why this may be happening. I am calling this class from an after update trigger Also, I am inserting customer history records using the same class too. The apex class is as below.
public without sharing class LineItemHistoryRecordCreation {
public LineItemHistoryRecordCreation() {}
public void createLineItemHistoryRecord(List<OrderItem> ordItemList) {
List<Id> oliIdList = new List<Id>();
List<History__c> historyList = new List<History__c>();
for(OrderItem oli : ordItemList) {
oliIdList.add(oli.Id);
History__c his = new History__c();
his.Change_Log__c = 'Order Product record deleted-OrderNumber ' + oli.OrderId + '-' + oli.Id + '-' + oli.OB_Order_ID__c;
his.History_Type__c = 'Order Product-Deleted';
his.History_DateTime__c = DateTime.now();
his.Record_Name__c = oli.Id;
his.Prior_Value__c = 'Active';
his.New_Value__c = 'Deleted';
historyList.add(his);
}
insert historyList;
List<OrderItem> oliList = [SELECT Id FROM OrderItem WHERE Id IN :oliIdList];
System.debug('order List akki' + oliList);
if(!oliList.isEmpty()) {
delete oliList;
}
}
}
Can someone please help me with understanding and solving this. Thanks in advance.
Upvotes: 0
Views: 3057