+ Start a Discussion
AviKesariAviKesari 

System.DmlException: INSUFFICIENT_ACCESS_OR_READONLY

Hi Folks,

I'm getting the following error:

Apex trigger TaskTrigger caused an unexpected exception, contact your administrator: TaskTrigger: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 0010c00001zZtfnAAC; first error: INSUFFICIENT_ACCESS_OR_READONLY, insufficient access rights on object id: []: ()

I have a trigger on Task and apex handler class which executes on after insert and after  update.

When a user logs a call either from account and contact(which have lookup relationship) then custom fields on account should get updated.

This works fine with sys admin credential but when i login and test as  a specific profile user. They get the error, the field is editable and apex class is enabled account has read, write and edit permission but not view all or modify all.

Can I make this code gets executed for any user and any profile meaning as a sys admin, How can I do this?
AviKesariAviKesari
My Code : I have to with sharing keyword on the class

public void updateAccountActivityFields(Task[] taskRecords){
       
        Set<Id> accIDs = new set<Id>();
        Set<String> conIDs = new Set<String>();
        
        for(Task t : taskRecords){
            conIDs.add(t.WhoID);
        } 
        
        Map<Id,Contact> conmap = new Map<Id,Contact>([SELECT AccountId FROM Contact WHERE Id IN :conIDs]);
        
        for(Contact c: conmap.values())
        {
        accIDs.add(c.AccountId);
        }
        List<Account> accountsToUpdate = [SELECT Id FROM Account WHERE Id =:accIDs];
       
        for(Task t: [SELECT Id, LastModifiedBy.name, type, LastModifiedDate, CreatedDate, RecordTypeId, WhoID, WhatID FROM task WHERE id in :taskRecords]){
            for(Account a : accountsToUpdate){
                Date createdDateJustDate = date.newinstance(t.CreatedDate.year(), t.CreatedDate.month(), t.CreatedDate.day());
                //update the activity fields on account with Task's lastModifiedDate and LastModifiedBy based on Task Type
                if(t.type=='Face 2 Face Meeting' || t.type=='Lunch'){
                    a.Last_Face_to_Face_Activity__c = createdDateJustDate;
                    a.Last_Visited_By__c = t.LastModifiedBy.name;
                }
                else{
                    a.Last_Call_Activity__c =  createdDateJustDate;
                }
            }
        }
        
        if(!accountsToUpdate.isEmpty() && accountsToUpdate.size() > 0){
            Update accountsToUpdate;       }
    }    
}
Raj VakatiRaj Vakati
You can try to fix the issue in two ways .. 
  1. Run your apex class in system mode with using with sharing  
  2. Or Grant the Modify all permission on the account object to the profile 
Deepali KulshresthaDeepali Kulshrestha
Hi AviKesari,

Greetings to you!

For updation you have to give ID also : -

if(t.type=='Face 2 Face Meeting' || t.type=='Lunch'){
    a.Last_Face_to_Face_Activity__c = createdDateJustDate;
    a.Last_Visited_By__c = t.LastModifiedBy.name;
    //a.Id=  --Here you have to give Id also to overwrite the data on particular Id.--
}
else{
    a.Last_Call_Activity__c =  createdDateJustDate;
    //a.Id=  --Here you have to give Id also to overwrite the data on particular Id.--
}

OR

- You can use map and matched the both Ids then update the object.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.