function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aditi Mohanty 5Aditi Mohanty 5 

2 users belong to 2 profiles. How to assign permission set to those profiles using apex only?

ANUTEJANUTEJ (Salesforce Developers) 
Hi Aditi,

>> 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.
Aditi Mohanty 5Aditi Mohanty 5
Can you please describe a little bit. I am new in APEX, so this is little bit hard for me to understand.
ANUTEJANUTEJ (Salesforce Developers) 
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.

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.
Aditi Mohanty 5Aditi Mohanty 5
Please explain it with code snippets, means how to do it with and without trigger and all. 
ANUTEJANUTEJ (Salesforce Developers) 
Can you specify the scenario so as to check further and respond with a sample code snippet.
ANUTEJANUTEJ (Salesforce Developers) 
Also, could you mention which line you are finding it difficult to understand so as to define further.
Aditi Mohanty 5Aditi Mohanty 5
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.
ANUTEJANUTEJ (Salesforce Developers) 
You can use the below trigger snippet and modify it as per your details:
 
trigger UTri on User (before update) {
    
    if(trigger.isbefore && trigger.isupdate)
    {
        list<PermissionSetAssignment> plist= new List<PermissionSetAssignment>();
        for(User u:trigger.new)
        {
            User oldval= Trigger.oldMap.get(u.Id);
            if(u.ProfileId != oldval.ProfileId)
            {
                PermissionSetAssignment ptemp= new PermissionSetAssignment();
                ptemp.PermissionSetId='';//add the id
                ptemp.AssigneeId=u.Id;
                plist.add(ptemp);
            }
        }
        if(plist.size()>0)
        {
            insert plist;
        }
    }

}

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.
ANUTEJANUTEJ (Salesforce Developers) 
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/
Aditi Mohanty 5Aditi Mohanty 5
Please provide code snippet for 1st scenario.
ANUTEJANUTEJ (Salesforce Developers) 
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.
Aditi Mohanty 5Aditi Mohanty 5
Can you please give answer for general , where we don't have to enter profile id maually. It will automatically be assigned when profile gets changed.
ANUTEJANUTEJ (Salesforce Developers) 
I don't see where we are giving the profile id manually in the above snippet.