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
- public class PermissionSetClass
- {
- public PermissionSetClass()
- {
- List<PermissionSetAssignment> perAssignments = [SELECT AssigneeId, PermissionSet.Name FROM PermissionSetAssignment WHERE PermissionSet.Name = 'AccountRating' AND AssigneeId=:UserInfo.getUserId()];
- if(!perAssignments.isEmpty())
- {
- Account acc = new Account();
- acc.Name = 'Test Account';
- insert acc;
- }
- }
- }
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
- @isTest
- private class TestPermissionSetClass
- {
- static testMethod void checkPermissionSet()
- {
- Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
- User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
- EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
- LocaleSidKey='en_US', ProfileId = p.Id,
- TimeZoneSidKey='America/Los_Angeles', UserName='testpermissionsetuser@testorg.com');
- insert u;
- // Query your permission set name from Organization that your want to test.
- PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = 'AccountRating'];
- // Assign the above inserted user for the above Permission Set.
- PermissionSetAssignment psa = new PermissionSetAssignment();
- psa.AssigneeId = u.Id;
- psa.PermissionSetId = ps.Id;
- insert psa;
- // Run your code with inserted user.
- System.runAs(u)
- {
- PermissionSetClass pr = new PermissionSetClass();
- }
- }
- }
Hello i am getting an error
ReplyDeleteInsert 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?
The same for me. I found workaround you just need to put permission set assignment in future method:
ReplyDelete@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);
}
Hey, thanks for the blog article.Really looking forward to read more. Cool.
ReplyDeletesalesforce online training from india
salesforce online training in bangalore