4

I have a custom object Resource__c having master detail with Account object. I have Account1 created by User1 and if User2 goes to create resource on Account1 then below error is given :"INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []".

How can we handle this error before inserting or upserting in our Apex class or do exception handling for this error.

  • 1
    first share your code. Second use try catch to handle exception . Third check Account OWD and profile permisssion. – Tushar Sharma Jan 21 '16 at 11:54
  • 1
    Can you edit that Account record from UI if you are logged in with User2? – Nitin Jan 21 '16 at 11:57
  • @Nitin - I can't edit that record, I agree that I cannot do any operation on that account but as I am having my Create new resource button in layout so I just want to know how to handle this error in my apex class so that error doesn't shown on user or some useful error should be displayed – Pravesh Mehta Jan 21 '16 at 12:33
  • @PraveshMehta I suggest before doing any DMl first check that current user having access to that record or not – Ratan Paul Jan 21 '16 at 13:38
  • @Ratan - it would be helpful if you can share some code. If I do such check, then sharing rules will cause any issue or not ? – Pravesh Mehta Jan 21 '16 at 14:02
7

You can use userRecordAccess object to check CRUD permissions..

So pass the recordId and userId.. to check that user having what type of access on that record..

you can check variables like HasDeleteAccess. If HasDeleteAccess true means user have delete access. If false then user don't have access..

Based on these accessibility perform DML..

List<UserRecordAccess> lstUserRecordAccess = [SELECT RecordId,MaxAccessLevel,HasAllAccess, 
                                                     HasDeleteAccess, 
                                                     HasEditAccess,
                                                     HasReadAccess, 
                                                     HasTransferAccess 
                                                FROM UserRecordAccess 
                                               WHERE UserId = '00550000000rlrX' 
                                                 AND RecordId = '0015000000TaWdI'];
| improve this answer | |
0

You can check accessible rights on any object by describing it as below:

Schema.DescribeSObjectResult accountDescribe = Account.sObjectType.getDescribe();
System.debug('accessible:' + accountDescribe.accessible);

If it is not accessible then, you can show a proper message to user.

I hope this will help.

| improve this answer | |
  • 1
    That is related to FLS, but here problem is related to ownership of record. I am not able to modify record whose owner is someone else so I just want to handle error in my apex code. – Pravesh Mehta Jan 21 '16 at 13:36

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.