So permission sets are assigned to users and not profiles so when you are assigning permissions sets to users via apex you can create an instance of PermissionSetAssignment which contains the following fields : AssigneeId, PermissionSetGroupId, PermissionSetId.
AssigneeId: for this field, you would be giving userid.
PermissionsSetId: for this field, you would be giving permissionsetid.
In this example, we are assigning a permissionset using apex we would be giving the id of both permission set and userid and then inserting this PermissionsSetAssignment.
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
there are 2 users, 2 profiles and 2 permission sets. An apex code that assign the permission set1 to user having profile1 and same for user2. For second one, if user's profile is changing to another then permission set to that new profile should be automatically be given. For this scenario i want an apex code with or without trigger, as soon as possible, as it is urgent.
other way to assign permission sets is to use flows, below is a link to that example: https://automationchampion.com/2020/03/03/getting-started-with-process-builder-part-96-profile-so-yesterday-auto-assign-permission-set-group-to-a-user/
you can use the below snippet and modify it accordingly:
if(trigger.isbefore && trigger.isinsert)
{
Profile profileid1=[select Id, Name from Profile where name =' system Amin'];
Profile profileid2=[select Id, Name from Profile where name =' system Amin'];
list<PermissionSetAssignment> plist= new List<PermissionSetAssignment>();
for(User u:trigger.new)
{
if(u.ProfileId == profileid1.id || u.ProfileId == profileid2.id)
{
PermissionSetAssignment ptemp= new PermissionSetAssignment();
ptemp.PermissionSetId='';//add the id
ptemp.AssigneeId=u.Id;
plist.add(ptemp);
}
}
if(plist.size()>0)
{
insert plist;
}
}
Please check accordingly and modify it to suit your details.
Please close the thread by marking this as the best answer so that it can be useful to others in the future.
>> https://salesforce.stackexchange.com/questions/60668/assigning-permission-set-to-user-in-apex
You can use the above link that has a snippet of how to assign using apex:
For quick reference check the below:
PermissionSetAssignment psa = new PermissionSetAssignment
(PermissionSetId = myPermissionSetId, AssigneeId = myAssigneeId);
insert psa;
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks.
AssigneeId: for this field, you would be giving userid.
PermissionsSetId: for this field, you would be giving permissionsetid.
PermissionSetAssignment psa = new PermissionSetAssignment
(PermissionSetId = myPermissionSetId, AssigneeId = myAssigneeId);
insert psa;
In this example, we are assigning a permissionset using apex we would be giving the id of both permission set and userid and then inserting this PermissionsSetAssignment.
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks.
For second one, if user's profile is changing to another then permission set to that new profile should be automatically be given.
For this scenario i want an apex code with or without trigger, as soon as possible, as it is urgent.
I am assigning the permission set when profile changes i,e., profileid is different from old profileid
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks.
Please check accordingly and modify it to suit your details.
Please close the thread by marking this as the best answer so that it can be useful to others in the future.