0

i am trying to update opportunity owner from custom owner field to standardrd owner field before performing this operation i want to check if owner has access to the record how can i check in apex

public with sharing class UpdateOwner {
    public static void UpdateOppty(List < Opportunity > listOpportunity, Map < Id, Opportunity > oldMap) {
        for (Opportunity Opp: listOpportunity) {
            if (opp.Owner__c != null && opp.Owner__c != oldMap.get(Opp.id).Owner__c) {
                if (Schema.sObjectType.Opportunity.fields.OwnerId.isUpdateable()) {
                    id usrid = opp.Owner__c;

                    List < Opportunity > Oppty = new List < Opportunity > ([SELECT Id, Name, Opportunity_Owner__c, UserRecordAccess.HasReadAccess, UserRecordAccess.HasTransferAccess, UserRecordAccess.MaxAccessLevel
                        FROM Opportunity
                    ]);
                    if (Oppty.Owner__c.MaxAccessLevel == true) {
                        Opp.OwnerId = opp.Owner__c;
                    }
                }
            }
        }
    }
1

you can check the record access using UserRecordAccess

Doing the SOQL on UserRecordAccess Object you can check the HasEditAccess,HasDeleteAccess,HasReadAccess access, So you will check in your code to add a condition with appropriate access you want to check. with userId(owner in your case) and recordid.

SELECT RecordId, HasEditAccess,HasDeleteAccess,HasReadAccess FROM UserRecordAccess WHERE UserId = :userid AND RecordId = :recId
1
  • make sure you are using valid ownerId(userid) , please run the above SOQL in anonymous window to test. – Sarvesh Sep 16 '19 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.