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
Katharine Anderson 13Katharine Anderson 13 

Check User ID against Permission Set Assignment in Trigger

So I have a trigger that I'm trying to write to update a value on a custom object when it is updated or created. The value should only be updated if the user does not have a certain permission set assigned to them. I have been trying to figure out how to check the user's ID against a list that contains the ids for the users that were assigned to that permision set, but I am fairly new to coding and googling is not working out. Maybe one of you can help? Here is my terrible code that isn't compiling: 
 
trigger UpdatePermissions on box__FRUP__c (before insert, before update) {
    //Create list of Easement Managers based on the Permission Set: 'Easement Management Fields'.
    List<PermissionSetAssignment> EasementManagers = [Select AssigneeId FROM PermissionSetAssignment WHERE PermissionSetId = '0PS0c000000Pi9z'];
    //Check User ID against Assignee ID. If not in list, change FRUP permission to  'Read' instead of 'Read/Write'.
   { for (box__FRUP__c frup : Trigger.New) {
        if (frup.box__Salesforce_User__r.ID IN EasementManagers)
			//do nothing
        } else {
            frup.box__Permission__c = 'Read';
        }
    }
}

 
Best Answer chosen by Katharine Anderson 13
Nikhil Verma 6Nikhil Verma 6
The issue here is that you are trying to compare a User ID (frup.box__Salesforce_User__r.ID) with Permission set assignment records since "EasementManagers" is a list of PermissionSetAssignment records. What you should do is get the user IDs from the list in a set and then check. Try the below code:
trigger UpdatePermissions on box__FRUP__c (before insert, before update) {
    //Create list of Easement Managers based on the Permission Set: 'Easement Management Fields'.
	Set<Id> setUserIds = new Set<Id>();
    List<PermissionSetAssignment> EasementManagers = [Select AssigneeId FROM PermissionSetAssignment WHERE PermissionSetId = '0PS0c000000Pi9z'];
	for(PermissionSetAssignment varPSA : EasementManagers)
		setUserIds.add(varPSA.AssigneeId);
    //Check User ID against Assignee ID. If not in list, change FRUP permission to  'Read' instead of 'Read/Write'.
	for (box__FRUP__c frup : Trigger.New) {
        if(setUserIds.contains(frup.box__Salesforce_User__r.ID)){
			//do nothing
        }else{
            frup.box__Permission__c = 'Read';
        }
    }
}

 

All Answers

Nikhil Verma 6Nikhil Verma 6
The issue here is that you are trying to compare a User ID (frup.box__Salesforce_User__r.ID) with Permission set assignment records since "EasementManagers" is a list of PermissionSetAssignment records. What you should do is get the user IDs from the list in a set and then check. Try the below code:
trigger UpdatePermissions on box__FRUP__c (before insert, before update) {
    //Create list of Easement Managers based on the Permission Set: 'Easement Management Fields'.
	Set<Id> setUserIds = new Set<Id>();
    List<PermissionSetAssignment> EasementManagers = [Select AssigneeId FROM PermissionSetAssignment WHERE PermissionSetId = '0PS0c000000Pi9z'];
	for(PermissionSetAssignment varPSA : EasementManagers)
		setUserIds.add(varPSA.AssigneeId);
    //Check User ID against Assignee ID. If not in list, change FRUP permission to  'Read' instead of 'Read/Write'.
	for (box__FRUP__c frup : Trigger.New) {
        if(setUserIds.contains(frup.box__Salesforce_User__r.ID)){
			//do nothing
        }else{
            frup.box__Permission__c = 'Read';
        }
    }
}

 
This was selected as the best answer
Katharine Anderson 13Katharine Anderson 13
Awesome, that makes sense. Thank you!