8

This new security method Security.stripInaccessible both strips out fields that the user has no rights to and will throw an exception if the object-level permissions are being violated.

What exception type is thrown (so I can catch and handle that specific type)?

7

It's a System.NoAccessException. The message is:

No access to entity: MyObject__c

Not sure the easiest way to verify, but I set up a Visualforce Page with the below controller so the running user didn't need appropriate permissions to execute anonymous Apex.

public with sharing class Demo
{
    public String type { get; private set; }
    public String message { get; private set; }
    public Demo()
    {
        try
        {
            Security.stripInaccessible(AccessType.CREATABLE, new List<NoPerm__c> {
                new NoPerm__c()
            });
        }
        catch (Exception e)
        {
            message = e.getMessage();
            type = e.getTypeName();
        }
    }
}

And the markup was simply:

<apex:page controller="Demo">
    <apex:pageMessage severity="FATAL"
                      summary="{!type}"
                      detail="{!message}" />
</apex:page>
5
  • Thanks, not in a position to test today and couldn't find it in the docs.
    – Keith C
    Feb 7 '20 at 17:44
  • Yeah, dunno if it's in the docs.
    – Adrian Larson
    Feb 7 '20 at 17:45
  • +1 Same thing that I did, you were just two minutes faster!
    – sfdcfox
    Feb 7 '20 at 17:46
  • 1
    @sfdcfox I guess I've still got it!
    – Adrian Larson
    Feb 7 '20 at 17:46
  • 4
    @KeithC The docs almost never tell you what exceptions can be thrown. It's one of the most annoying aspects of the docs.
    – sfdcfox
    Feb 7 '20 at 17:46

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.