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
Shivom Verma 35Shivom Verma 35 

Assign permission set to user using trigger

Write a Trigger when Admin creating new user if he select chekbox (create new ReadEditPermissionset on Account) assign that permission to that new user,if systemAdministrator deselect ,Remove that permission.
SwethaSwetha (Salesforce Developers) 
HI Shivom,
Based on https://trailblazer.salesforce.com/ideaView?id=08730000000DrBwAAK currently, it is not possible to have a trigger on PermissionSetAssignment.

Related : https://salesforce.stackexchange.com/questions/224286/trigger-on-permissionsetassignment-object-and-alternatives/224289

If this information helps, please mark the answer as best. Thank you
Oshin AgrawalOshin Agrawal
Hi,

I would suggest you use this on the user trigger, I have used a boolean field 'assign_permission__c' on user object.
Supposingly you will need to make few changes according to your requirement: 
if(trigger.isupdate)
    {
        if(trigger.isafter)
        {
            List<PermissionSetAssignment> permissionSetinsertList = new List<PermissionSetAssignment>();
            list<id> lstuseridtoberemoved = new list<id>();
            list<PermissionSet> permissionstobeassigned = [SELECT Id, Label FROM PermissionSet WHERE Label = 'ReadEditPermissionset'];
            for(User u: trigger.new)
            {
                boolean oldstate = trigger.oldmap.get(u.id).assign_permission__c;
                if(u.assign_permission__c != oldstate && u.assign_permission__c == true)
                {
                    PermissionSetAssignment psa = new PermissionSetAssignment(PermissionSetId = permissionstobeassigned[0].id , AssigneeId = u.Id);
					permissionSetinsertList.add(psa);
                }
                else if(u.assign_permission__c != oldstate && u.assign_permission__c == false)
                {
					 lstuseridtoberemoved.add(u.id);                   
                }
            }
            if(permissionSetinsertList.size()>0)
            {
                insert permissionSetinsertList;
            }
            if(lstuseridtoberemoved.size()>0)
            {
                delete [SELECT Id from PermissionSetAssignment WHERE PermissionSetId = : permissionstobeassigned[0].id AND AssigneeId in : lstuseridtoberemoved];
            }
        }
    }

Thanks
Shivom Verma 35Shivom Verma 35
We need to create a trigger on user object not on PermissionsetAssignment object
 
David Ferrell 7David Ferrell 7
This is a difficult enough task to complete. I ran into similar ones when I was in college. Not sure if I could handle this on my own now.  Then I worked it out using https://uk.edubirdie.com/assignment-expert where looking for assignment expert. I overestimated my strength by choosing this particular industry to study. But thanks to various useful resources, I was still able to complete my studies successfully.