2

anyone know how to throw an access level exception in a test class to cover the below code sample? I'm assuming the object being inserted with Database.insert needs to have sharing settings modified somehow but thought I'd ask someone smarter than me :)

The object being inserted is Application__Share

if (newAppShrs.size() > 0) {
    Database.SaveResult[] lsr = Database.insert(newAppShrs,false);

    List<String> errorMessages = new List<String>();
    // Process the save results for insert.
    for(Database.SaveResult sr : lsr){
        if(!sr.isSuccess()){
            // Get the first save result error.
            Database.Error err = sr.getErrors()[0];

            // Check if the error is related to trivial access level.
            // Access levels equal or more permissive than the object's default
            // access level are not allowed.
            // These sharing records are not required and thus an insert exception
            // is acceptable.
            if(!(err.getStatusCode() == StatusCode.FIELD_INTEGRITY_EXCEPTION
                    && err.getMessage().contains('AccessLevel'))) {
                // Error is not related to trivial access level.
                // Send an email to the Apex job's submitter.
                errorMessages.add(err.getMessage());
            }
        }
    }
1
  • 1
    So you want to test a scenario where the user wants to, say, give permission for an Application record they do not have access to, or are not authorised to provide access to others? Like manually sharing the record? Mar 31 '19 at 1:21
1

One of the ways to do that is creating user without needed privileges in test class, for example, chatter user.

Then in test method use:

System.runAs(insertedUser) {}

Link on documentation : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm

0

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.