Use Case:

Pre-requisite: Create a permission set with API name Sales_User_Permissions.

Whenever a new user is created with profile Custom: Sales Profile, assign the permission set Sales_User_Permissions to User.

Queueable apex class can be created for this which would implement the Queueable interface. We can then submit our Queueable class for execution from UserTrigger and system will process the job when the resources become available.

trigger UserTrigger on User (after insert) {
if(Trigger.isAfter && Trigger.isInsert) {
// Call the Queueable class method which would assign the permission set to user
UserQueueable queueableJob = new UserQueueable(Trigger.new);
Id jobId = System.enqueueJob(queueableJob);
System.debug(jobId);
}
}

public class UserQueueable implements Queueable {
public List<User> users;
// Assign the list of Users from trigger to the list 'users'
public UserQueueable(List<User> users) {
this.users = users;
}
public void execute(QueueableContext context) {
// Get the profile record
Profile salesProfile = [Select Id From Profile Where Name = 'Custom: Sales Profile' LIMIT 1];
// Get the Permission Set which needs to be assigned to the new User
PermissionSet salesUserPermissionSet = [Select Id From PermissionSet Where Name = 'Sales_User_Permissions'];
// Create the PermssionSetAssignment record using permission set and the new user inserted
List<PermissionSetAssignment> psAssignments = new List<PermissionSetAssignment>();
if(salesUserPermissionSet != null) {
for(User usr : users) {
if(usr.ProfileId == salesProfile.Id) {
psAssignments.add(new PermissionSetAssignment(PermissionSetId = salesUserPermissionSet.Id,
AssigneeId = usr.Id));
}
}
}
// Create the record for PermssionSetAssignments
if(psAssignments != null) {
insert psAssignments;
}
}
}

Test Class:

Scenario 1- Create a new user with profile “Custom: Sales Profile”. Verify that after insertion of the user, a permission set assignment record should be created with ‘Sales_User_Permissions’ permission set and inserted User as the assignee.

Scenario 2- Create a new user with profile “Standard User”. Verify that after insertion of the user, no permission set assignment record should be created with ‘Sales_User_Permissions’ permission set and inserted User as the assignee.

@isTest
public class TestUserUtil {
public static User createUser(String fName, String lName, String alias, Profile profile) {
User usr = new User(LastName = lName,
FirstName = fName,
Alias = alias,
Email = fName + '.' + lName + '@apexstepbystep.com',
Username = 'test.user444@apexstepbystep.com',
ProfileId = profile.id,
TimeZoneSidKey = 'GMT',
LanguageLocaleKey = 'en_US',
EmailEncodingKey = 'UTF-8',
LocaleSidKey = 'en_US'
);
return usr;
}
}

@isTest
private class UserTriggerTest {
// Create a new user with profile "Custom: Sales Profile"
// Verify that after insertion of the user, a permission set assignment record should be created
// with 'Sales_User_Permissions' permission set and inserted User as the assignee.
@isTest
static void test_afterInsert_validProfile() {
Profile profile = [SELECT Id FROM Profile WHERE Name = 'Custom: Sales Profile' LIMIT 1];
User usr = TestUserUtil.createUser('Test', 'User444', 'tusr', profile);
Test.startTest();
    insert usr;
Test.stopTest();

        PermissionSet ps = [Select Id From PermissionSet Where Name = 'Sales_User_Permissions' LIMIT 1];
// Verify that permission set should be assigned to the user
List<PermissionSetAssignment> psa = [Select Id From PermissionSetAssignment Where PermissionSetId = :ps.Id AND AssigneeId = :usr.Id];
System.assertEquals(1, psa.size(), 'Permission set records are not as expected.');
}
// Create a new user with profile "Standard User"
// Verify that after insertion of the user, no permission set assignment record should be created
// with 'Sales_User_Permissions' permission set and inserted User as the assignee.
@isTest
static void test_afterInsert_inavlidProfile() {
Profile profile = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
User usr = TestUserUtil.createUser('Test', 'User444', 'tusr', profile);
Test.startTest();
    insert usr;
Test.stopTest();

        PermissionSet ps = [Select Id From PermissionSet Where Name = 'Sales_User_Permissions' LIMIT 1];
// Verify that permission set should be assigned to the user
List<PermissionSetAssignment> psa = [Select Id From PermissionSetAssignment Where PermissionSetId = :ps.Id AND AssigneeId = :usr.Id];
System.assertEquals(0, psa.size(), 'Permission set records are not as expected.');
}
}
Code Coverage: 100%
Like it ? Share : Do Nothing

Leave a Reply