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
sfdc98sfdc98 

delete scenario

how to delete permissionsetA to the user when user changes roleA changes to roleB using trigger.

Thanks in Advance.
CharuDuttCharuDutt
Hii SFDC
Try Below Code
trigger DeletePermissionAssignment on User (After Update) {
    Set<Id> usrId = new Set<Id>();
    for(User u :  trigger.new){
        if(u.UserRoleId != Trigger.oldMap.get(u.Id).UserRoleId){
           usrId.add(u.Id);
        }
    }
	list<PermissionSetAssignment> lstPerm = [SELECT Id, PermissionSetId, PermissionSetGroupId, AssigneeId, IsActive 
                                             FROM PermissionSetAssignment WHERE AssigneeId IN :usrId ];
    if(lstPerm.size()>0){
        delete lstPerm;
    }
}
Please Mark It As Best Asnwer If It Helps
Thank You!
sfdc98sfdc98
@charuDutt ,thanks for your reply , my requirement is need to assign  permissionsetA when user record is inserted with roleA and when user record updates with roleB need to assign permissionsetB this is achieved sucessfully, now when i change roleA to roleB need to delete permissionsetA and add permissionsetB and vice versa , am getting below error when trying to change role in user record

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger assigningpermission caused an unexpected exception, contact your administrator: assigningpermission: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, Duplicate PermissionSetAssignment. Assignee: 0052v00000hRRwk; Permission Set: 0PS2v000006IUrs: [AssigneeId, PermissionSetId]: Trigger.assigningpermission: line 10, column 1


mytrigger:

trigger assigningpermission on User (after insert,after update,before delete,after delete) {
user us = [select id, UserroleId from user where id in :trigger.new];
  PermissionSet ps =  [SELECT Id FROM PermissionSet
                         WHERE Id = '0PS2v000006IUrsGAG'];
    PermissionSet ps1=[select Id from permissionset where Id='0PS2v000006IUrxGAG'];
    //RoleA id
      if(us.UserroleId == '00E2v000002SZA7' ){                 

PermissionSetAssignment psa = new PermissionSetAssignment(PermissionSetId = ps.id, AssigneeId = us.id);
 insert psa;
         
      system.debug(psa.AssigneeId);     
    }
    //roleB id
    if(us.UserRoleId=='00E2v000002SZAC'){
      PermissionSetAssignment psa1 = new PermissionSetAssignment(PermissionSetId = ps1.id, AssigneeId = us.id);
insert psa1;
    
    }
//deleting permissionset 
    Set<Id> usrId = new Set<Id>();
    for(User u :  trigger.new){
        if(u.UserRoleId != Trigger.oldMap.get(u.Id).UserRoleId){
           usrId.add(u.Id);
        }
    }
    list<PermissionSetAssignment> lstPerm = [SELECT Id, PermissionSetId, PermissionSetGroupId, AssigneeId, IsActive 
                                             FROM PermissionSetAssignment WHERE AssigneeId IN :usrId ];
    if(lstPerm.size()>0){
        delete lstPerm;
    }
    
}

Thanks in Advance