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.

Consider this not urgent and permission set can be assigned when the resource is available in salesforce org. Use @future annotation for the method which contains the logic.

trigger UserTrigger on User (after insert) {
if(Trigger.isAfter && Trigger.isInsert) {
Set<Id> userIds = new Set<Id>();
// Get the profile record
Profile salesProfile = [Select Id From Profile Where Name = 'Custom: Sales Profile' LIMIT 1];
// Add the user record to set only if the profile is 'Custom: Sales Profile'
for(User user : Trigger.new) {
if(user.ProfileId == salesProfile.Id) {
userIds.add(user.Id);
}
}
// Use UserTriggerHandler to create PermssionSetAssignment records
if(userIds != null) {
UserTriggerHandler.assignSalesPermissionSet(userIds);
}
}
}


public class UserTriggerHandler {
// Since it is not necessary that user should have permission set instantly as soon as the User is created
// So here, we can use future method.
// @Param, set of user Ids which are inserted having profile 'Custom: Sales Profile'
@future
public static void assignSalesPermissionSet(Set<Id> newUsersId) {
// 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(Id userId : newUsersId) {
psAssignments.add(new PermissionSetAssignment(PermissionSetId = salesUserPermissionSet.Id,
AssigneeId = userId));
}
}
// 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