1

I have a validation rule

AND(
    ISPICKVAL(Submission_Handover_Process_Status__c, 'Awaiting Validation'),
    ISCHANGED(Submission_Handover_Process_Status__c),
    NOT($Permission.Submission_Handover)
)

I have permission set with the above custom permission set:

Permission set Screenshot

My issue is when I am trying to assign the permission set to the user I am creating in the test class to bypass the validation, but that validation is always getting fired because of the Permission, resulting in test failures.

User usr = wtclm_UTIL_TestDataFactory.insertTestUser(DAL_Profile.PROFILE_SYS_ADMIN, DAL_User.USER_TYPE_STANDARD);
        System.runAs(usr) {
            PermissionSet submissionPermissionSet = [SELECT Id FROM PermissionSet WHERE Name = 'PermsetName'];
            if (submissionPermissionSet != null) { insert new PermissionSetAssignment(AssigneeId = usr.id, PermissionSetId = submissionPermissionSet.Id);claim.Submission_Handover_Process_Status__c = wtclm_ClaimsSelector.HANDOVER_STATUS_AWAITING_VALIDATION;
        update claim;
}
10
  • That is how you would do it. Just a note, if (submissionPermissionSet != null) is equivalent to if (true) and can be removed. SOQL never returns null.
    – Adrian Larson
    Oct 17, 2022 at 12:12
  • @AdrianLarson But it is not working for me it works only when I assigned the custom permission via the profile Oct 17, 2022 at 12:17
  • on which object do you have this validation rule? Oct 17, 2022 at 12:34
  • It is on Case Object Oct 17, 2022 at 12:39
  • One thing that stands out is, it is odd to use runAs to insert the PermissionSetAssignment. It might indicate you used it there instead of where you need it, which is the operation against Case.
    – Adrian Larson
    Oct 17, 2022 at 12:43

1 Answer 1

1

I am able to do it by assigning the permission set in test setup method (i.e. the one with the @testSetup annotation).

2
  • Can you expand on this a little? You mean in a method marked @testSetup right?
    – Matt Lacey
    Oct 31, 2022 at 15:55
  • @MattLacey Exactly Nov 1, 2022 at 5:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .