Tuesday, 5 May 2015

How to test your Permission Set logic in Apex Test Class


          Here is the simple example to test your permission set logic in your test class.  The below class will query the Permission set named as "AccountRating" and Checking if the logged in user available in that permission set. If yes, i am creating account record.

Apex Class

  1. public class PermissionSetClass
  2. {
  3.     public PermissionSetClass()
  4.     {
  5.         List<PermissionSetAssignment> perAssignments = [SELECT AssigneeId, PermissionSet.Name FROM PermissionSetAssignment WHERE   PermissionSet.Name = 'AccountRating' AND AssigneeId=:UserInfo.getUserId()];
  6.         if(!perAssignments.isEmpty())
  7.         {
  8.             Account acc = new Account();
  9.             acc.Name = 'Test Account';
  10.             insert acc;
  11.         }    
  12.     }
  13. }


        To write a test class for the above logic the following thing is needed. 

Step 1: Insert a test user record.
Step 2: Query your Permission Set information that you have used in class.
Step 3: Assign the inserted user to the above queried Permission Set. For this PermissionSetAssignment object is used.
Step 4:  Initialize your class/method by running as the above inserted user. 

Test Class

  1. @isTest
  2. private class TestPermissionSetClass
  3. {
  4.     static testMethod void checkPermissionSet()
  5.     {
  6.         Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
  7.        
  8.         User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
  9.                           EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
  10.                           LocaleSidKey='en_US', ProfileId = p.Id,
  11.                           TimeZoneSidKey='America/Los_Angeles',     UserName='testpermissionsetuser@testorg.com');
  12.         insert u;
  13.        
  14.         // Query your permission set name from Organization that your want to test.
  15.         PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = 'AccountRating'];
  16.        
  17.         // Assign the above inserted user for the above Permission Set.
  18.         PermissionSetAssignment psa = new PermissionSetAssignment();
  19.         psa.AssigneeId = u.Id;
  20.         psa.PermissionSetId = ps.Id;
  21.         insert psa;
  22.        
  23.         // Run your code with inserted user.
  24.         System.runAs(u)
  25.         {
  26.             PermissionSetClass pr = new PermissionSetClass();
  27.         }
  28.     }
  29.    
  30. }

3 comments:

  1. Hello i am getting an error

    Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): PermissionSetAssignment, original object: UserGroupDepartmentTeamHistory__c: []

    Can you please help?

    ReplyDelete
  2. The same for me. I found workaround you just need to put permission set assignment in future method:
    @testSetup
    static void setup() {
    String uniqueUserName = 'testuser' + DateTime.now().getTime() + '@testorg.com';
    Profile prof = [
    SELECT Id
    FROM Profile
    WHERE Name = 'Standard User'
    LIMIT 1
    ];
    system.debug(prof);
    User user = new User(Alias = 'TestUser', Email = 'testuser@testorg.com', EmailEncodingKey = 'UTF-8', LastName = 'Testing', LanguageLocaleKey = 'en_US', LocaleSidKey = 'en_US', ProfileId = prof.Id, TimeZoneSidKey = 'America/Los_Angeles', UserName = uniqueUserName);
    insert user;
    AccountTrigger_Test.addPermSet();
    }
    @future
    private static void addPermSet() {
    List users = [SELECT Id FROM User WHERE Alias = 'TestUser'];
    List ps = [SELECT Id, Name FROM PermissionSet WHERE Name = 'Loyalty_Program'];
    insert new PermissionSetAssignment(AssigneeId = users[0].id, PermissionSetId = ps[0].Id);
    }

    ReplyDelete

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...