You need to sign in to do that
Don't have an account?
I wrote a code to add Permission Set to user which to assigned to particular profile. But the code is not working. Can some one please help where is wrong in this code.
Trigger>
trigger TriggerOnUserToAddPermissionSet on User (After insert) {
if(trigger.isAfter && trigger.isInsert){
UserHandlerClass.AfterInsert(trigger.new);
}
}
public class UserHandlerClass {
public static void AfterInsert(list<user> listOfUsers){
set<Id> setOfUserIds = new Set<Id>();
String PermissionSetName = 'Assign_Lead_Permissions';
for(User us : listOfUsers){
if(us.IsActive == true && us.Profile.Name == 'sys Admin'){
setOfUserIds.add(us.Id);
system.debug('Users are added to Set');
}
}
PermissionSet permissionSetId = [select id,Name from PermissionSet where name ='Assign_Lead_Permissions'];
list<PermissionSetAssignment> listOfPermissionSetAssignmentToUsers = new list<PermissionSetAssignment>();
list<User> listOfUser = [SELECT Id,Name from User where Id IN:setOfUserIds ];
for(User u:listOfUser ){
PermissionSetAssignment Assign = new PermissionSetAssignment();
Assign.AssigneeId = u.Id;
Assign.PermissionSetId = permissionSetId.id;
listOfPermissionSetAssignmentToUsers.add(Assign);
system.debug('Assigned to Users');
}
if(!listOfPermissionSetAssignmentToUsers.isEmpty()){
insert listOfPermissionSetAssignmentToUsers;
}
}
}
Can you check if you have a profile with this name and creating a user with that profile.
us.Profile.Name == 'sys Admin'. Usually system admin will have '
System Administrator' name.
If this solution helps, Please mark it as best answer.
Thanks,
Yes I have. I have cloned Sytem Administator and changed the Name. I created user and assigned that profile.
Try Below Trigger Please Mark It As Best Answer If It Helps
Thank You!