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
sandy ksandy k 

How to check whether the user have edit access on opportunity using apex code

Hi Guys

How to check whether the user have edit access on opportunity before editing the opportunity using apex code. How can this be implemented using CRUD operation.
PriyaPriya (Salesforce Developers) 
Hey Sandy,

To find out if a particular user has Edit access to a record, use the UserRecordAccess object. This object is available in API version 24.0 and later. You can use SOQL to query this object to find out if the user has edit access to the record in question
 
SELECT RecordId, HasEditAccess 
FROM UserRecordAccess 
WHERE UserId = [single ID] AND RecordId = [single ID]

If you want to check a batch of records you can use
 
SELECT RecordId 
FROM UserRecordAccess 
WHERE UserId=:UserInfo.getUserId() 
    AND HasReadAccess = true 
    AND RecordId IN :allRecordIds 
LIMIT 200


But make sure that allRecordIds is a LIST of IDs. It doesn't work if allRecordIds is a SET of IDs. I guess that's a bug.

Also, only a maximum amount of 200 recordIds can be checked in one query.

Kindly mark it as the best answer if the information was helpful so that it can help others as well.

Regards & Thanks,

Priya Ranjan

Bryan Leaman 6Bryan Leaman 6
Good information to know, Priya!
If the OP just wants to know if the user has edit authority on the opportunity object itself, that can be done via:
Schema.Opportunity.SObjectType.getDescribe().isCreateable() and Schema.Opportunity.SObjectType.getDescribe().isUpdateable()
mukesh guptamukesh gupta
Hi Sandy,

Please follow below code:-
 
SELECT Id, Name, UserRecordAccess.HasReadAccess FROM Opportunity Where Id = 'OpportuityId'
SELECT RecordId FROM UserRecordAccess WHERE UserId=:UserInfo.getUserId() AND HasReadAccess = true AND RecordId IN :allRecordIds LIMIT 200



if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh