function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfadm sfdevsfadm sfdev 

Why is the INSUFFICIENT_ACCESS_OR_READONLY, insufficient access rights on object id: []

Hello,

Could you please help me understand why I receive the following error:
Delete failed. First exception on row 0 with id 04Z7E000000596S; first error: INSUFFICIENT_ACCESS_OR_READONLY, insufficient access rights on object id: []
Error is in expression '{!processRecord}' in component <apex:page> in page unlockopportunityrecord: External entry point
every time I try to unlock an Opportunity record.
The code in the apex class that I'm using is:
public with sharing class UnlockRecordDuringApprovalController {
        Opportunity objOpportunity;
        String objOpp;
	
        public UnlockRecordDuringApprovalController(ApexPages.StandardController controller) {
    	objOpportunity = (Opportunity)controller.getRecord();
	}
	
       public void processRecord() {
        Approval.UnlockResult unlockedRersult = Approval.unlock(objOpportunity);
        // Iterate through each returned result
        if (unlockedRersult.isSuccess()) {
            // Operation was successful, so get the ID of the record that was processed
            System.debug('Successfully unlocked opportunity with ID: ' + unlockedRersult.getId());
        }
        else {
            // Operation failed, so get all errors                
            for(Database.Error err : unlockedRersult.getErrors()) {
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('opportunity fields that affected this error: ' + err.getFields());
            }
        }
    }    
}
and the Visual Force page code is:
<apex:page standardController="opportunity" extensions="UnlockRecordDuringApprovalController"  action="{!processRecord}" >
</apex:page>
Could you please explain what is causing this error to appear and prevent the unlock of the Opportunity record?

It is quite urgent to find a solution.

Thank you


 
Best Answer chosen by sfadm sfdev
Prateek Singh SengarPrateek Singh Sengar
Hi,
Try removing the with sharing clause from your apex class. It looks like the user who is trying to unlock record doesnt have the permission to do so. 
 

All Answers

Prateek Singh SengarPrateek Singh Sengar
Hi,
Try removing the with sharing clause from your apex class. It looks like the user who is trying to unlock record doesnt have the permission to do so. 
 
This was selected as the best answer
sfadm sfdevsfadm sfdev
Hi PrateekSinghSengar,

It really worked.

Thank you so much.
sfadm sfdevsfadm sfdev
Hi PrateekSinghSengar,

This time however I'm having this error message:
Delete failed. First exception on row 0 with id 0Pa7E000001PBlTSAW; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): PermissionSetAssignment, original object: EntityLock: [] Error is in expression '{!processRecord}' in component in page unlockopportunityrecord: Class.UnlockRecordDuringApprovalController.processRecord: line 27, column 1


I'm using the following code to unlock the Opportunity record:

public class UnlockRecordDuringApprovalController {
Opportunity objOpportunity;
// String objOpp;
String currentuserId = UserInfo.getUserId();

public UnlockRecordDuringApprovalController(ApexPages.StandardController controller) {
    objOpportunity = (Opportunity)controller.getRecord();
}

public void processRecord() {
    Approval.UnlockResult unlockedRersult = Approval.unlock(objOpportunity);
    // Iterate through each returned result
    if (unlockedRersult.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully unlocked opportunity with ID: ' + unlockedRersult.getId());
    }
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : unlockedRersult.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('opportunity fields that affected this error: ' + err.getFields());
        }
    }
    System.debug('currentuserId ' + currentuserId);
    List<PermissionSetAssignment> listPermissionSets = [SELECT Id, AssigneeId,       PermissionSetId FROM PermissionSetAssignment WHERE AssigneeId = :currentuserId AND PermissionSetId IN (SELECT Id FROM PermissionSet WHERE IsOwnedByProfile = false)];
    delete listPermissionSets;
    PermissionSetAssignment psa = new PermissionSetAssignment(PermissionSetId = '0PS7E000000DBGA', AssigneeId = currentuserId);     
    System.debug('psa ' + psa);
    insert psa;
    System.debug('psa7 ' + psa);
}
I have created my own custom button and when the button is pressed the above code is executed but I receive the following error message:
Delete failed. First exception on row 0 with id 0Pa7E000001PBlTSAW; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): PermissionSetAssignment, original object: EntityLock: [] Error is in expression '{!processRecord}' in component in page unlockopportunityrecord: Class.UnlockRecordDuringApprovalController.processRecord: line 27, column 1
Could you please advise what is the cause of such error message and how it can be avoided?

Thank you
 
Duncan Barr 2Duncan Barr 2
Hey guys,

This was a great help and certainly solved my issue for now, but I imagine Salesforce will eventually crack down on using the With Sharing keyword for things like this. Is there a permission that would allow a user to unlock a record with Approval.unlock(id) other than Modify All Data?

Thanks!
Duncan