Use Case:

Write a trigger:

Whenever a new User having profile “System Administrator” is inserted and is Active, add the user to the public group “Admins”.

Pre-requisite: Create a public group with name as “Admins”.

trigger UserTrigger on User (after insert) {
if(Trigger.isAfter) {
if(Trigger.isInsert) {
UserTriggerHandler.addAdminToPublicGroup(Trigger.new);
}
}
}


public class UserTriggerHandler {
public static void addAdminToPublicGroup(List<User> newUsers) {
// Get Id of Public group for Admins
Group adminGroup = [SELECT Id FROM Group WHERE DeveloperName = 'Admins' AND Type = 'Regular' LIMIT 1];
// Perform operation of adding Admin users to the group only when Group is present in salesforce
if(adminGroup != NULL) {
List<GroupMember> groupMembers = new List<GroupMember>();
// Get admin proifle and check for all users,
// if user profile id is same as the admin profile id AND user is active then only create a new group member
Profile adminProfile = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1];
for(User user : newUsers) {
if(user.ProfileId == adminProfile.Id && user.IsActive == true) {
groupMembers.add(new GroupMember(GroupId = adminGroup.Id,
UserOrGroupId = user.Id));
}
}
insert groupMembers;
}
}
}

Test Class:

Scenario 1- Verify that when user having System Administrator profile is created, the user is also added to the public group Admin.

Scenario 2- Verify that when user having other than the System Administrator profile is created, the user should not be added to the public group Admin.

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

@isTest
private class UserTriggerTest {
// Verify that when user having System Administrator profile is created, the user is also added to the public group Admin.
@isTest
static void testUserInsertion_valid() {
Group adminGroup = [SELECT Id FROM Group WHERE DeveloperName = 'Admins' AND Type = 'Regular' LIMIT 1];
// GroupMembers before insertion
List<GroupMember> membersBefore = [SELECT Id From GroupMember Where GroupId = :adminGroup.Id];
Profile adminProfile = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1];
User usr = TestUserUtil.createUser('Test', 'User', 'tusr', 'test.user444@apexstepbystep.com', adminProfile);
Test.startTest();
insert usr;
Test.stopTest();
// Assert that members after insertion should be exactly 1 greater than before
List<GroupMember> membersAfter = [SELECT Id, UserOrGroupId From GroupMember Where GroupId = :adminGroup.Id];
System.assertEquals(membersBefore.size() + 1, membersAfter.size(), 'User is not added to group Admins.');
Set<Id> userIdsSet = new Set<Id>();
for(GroupMember member : membersAfter) {
userIdsSet.add(member.UserOrGroupId);
}
System.assertEquals(true, userIdsSet.contains(usr.Id), 'GroupMember is not created for the inserted user.');
}
// Verify that when user having other than the System Administrator profile is created, the user should not be added to the public group Admin.
@isTest
static void testUserInsertion_invalid() {
Group adminGroup = [SELECT Id FROM Group WHERE DeveloperName = 'Admins' AND Type = 'Regular' LIMIT 1];
// GroupMembers before insertion
List<GroupMember> membersBefore = [SELECT Id From GroupMember Where GroupId = :adminGroup.Id];
Profile adminProfile = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
User usr = TestUserUtil.createUser('Test', 'User', 'tusr', 'test.user444@apexstepbystep.com', adminProfile);
Test.startTest();
insert usr;
Test.stopTest();
// Assert that members after insertion should be same as before
List<GroupMember> membersAfter = [SELECT Id, UserOrGroupId From GroupMember Where GroupId = :adminGroup.Id];
System.assertEquals(membersBefore.size(), membersAfter.size(), 'Group Members count is not same.');
Set<Id> userIdsSet = new Set<Id>();
for(GroupMember member : membersAfter) {
userIdsSet.add(member.UserOrGroupId);
}
System.assertEquals(false, userIdsSet.contains(usr.Id), 'GroupMember is created for the inserted user.');
}
}
Code Coverage: 100%
Like it ? Share : Do Nothing

Leave a Reply