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
sriram k 15sriram k 15 

I have written apex code When User is Deactivated then Assigned Permission Set should be deleted before update by using trigger? Test code coverage is done and unable to pass Assertion. Can some one help me how to pass Assertion statement?

Trigger Class >>>>>>>>

rigger TriggerOnUser on User (before update) {

    HandlerClass_User.userisDeactivated(, trigger.oldMap);

Handler Class >>>>>>>>>

public class HandlerClass_User {

    public static void userisDeactivated(list<User> newUser, Map<Id,User>OldMap){
        set<Id> setOfDeactivedIds = new Set<Id>();
        Map<Id,list<PermissionSetAssignment>> userPSAMap = new Map<Id,list<PermissionSetAssignment>>();        
        list<PermissionSetAssignment> PSAuserDelete = new list<PermissionSetAssignment>();        
        for(User u : newUser){
            if(u.IsActive == false && u.IsActive != OldMap.get(u.Id).IsActive){
                system.debug('Deactived User Id is';
        list<PermissionSetAssignment> listOfPremissionSetAssign = [SELECT Id, AssigneeId,PermissionSetId,PermissionSet.Name  from PermissionSetAssignment
                                                Where AssigneeId IN : setOfDeactivedIds AND Permissionset.IsOwnedByProfile =false];
        List<PermissionSetLicenseAssign> permSetsLiceUsers = [SELECT Id, AssigneeId from PermissionSetLicenseAssign where AssigneeId IN: setOfDeactivedIds];
        List<GroupMember> grpMemUsers = [SELECT Id, GroupId, UserOrGroupId FROM GroupMember WHERE UserorGroupId IN : setOfDeactivedIds]; 
        for(User u:newUser){
            if(u.IsActive == false && u.IsActive != OldMap.get(u.Id).IsActive){
                for(PermissionSetAssignment psa : listOfPremissionSetAssign){
                    if(psa.AssigneeId =={
                            userPSAMap.put(, new list<PermissionSetAssignment> {psa});
        system.debug('Get user Id and PSA'+userPSAMap);
        for(User u: newUser){
            if(u.IsActive == false && u.IsActive != OldMap.get(u.Id).IsActive){
        system.debug('List Of PSA to Delete for Users'+PSAuserDelete);
        delete PSAuserDelete;

Test Class >>>>>>>>>>>>

public class TestHandlerClass_User {

    public static void TestDeactiveUser(){
        Profile p = [SELECT Id,Name FROM Profile where Name='sys Admin'];
        UserRole u1 =[SELECT Id,Name FROM UserRole where Name='Manager'];
        User u = new User(UserName='', Alias = 'rkall', Email='', 
                          EmailEncodingKey='UTF-8', LastName='Kallam', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US',ProfileId = p.Id,UserRoleId,TimeZoneSidKey='America/Los_Angeles',isActive =true);
       // insert u;
        list<User> lu = new List<User>();
        insert lu;
        system.debug('list users'+lu);
        system.debug('list users'+lu[0].id);
         Id AssiId = lu[0].id;
        Map<Id,User> Mu = new Map<Id,User>();
        Mu.put(lu[0].id, lu[0]);
        system.debug('Map Key'+ Mu.keySet());        
        list<PermissionSetAssignment> lpsa = [SELECT Id, AssigneeId, PermissionSetId,PermissionSet.Name FROM PermissionSetAssignment where PermissionSet.Name like '%Assign%'];
          List<PermissionSetAssignment> psl = new List<PermissionSetAssignment>();
            PermissionSetAssignment psa =new PermissionSetAssignment(PermissionSetId = lpsa[0].PermissionSetId, AssigneeId = lu[0].Id);
            insert psl;  
        lu[0].IsActive= false;
        update lu;
      //  HandlerClass_User hc = new HandlerClass_User();
       List<PermissionSetAssignment> lpsa1 = [SELECT Id, AssigneeId,PermissionSetId,PermissionSet.Name  from PermissionSetAssignment where AssigneeId =: Mu.keySet() ];
         System.assertEquals(lpsa1.isEmpty(), true);

SwethaSwetha (Salesforce Developers) 
HI sriram,

Your ask appears duplicate of

Please let me know if I can help with any other information.Thanks
sriram k 15sriram k 15

Hi Swetha,

I can able to see the Test covergae but In Assertion unable to get Expected Result. Assertion is Failing. Can you please help on that.

 System.assertEquals(lpsa1.isEmpty(), true);

Thanks ,