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 Class is not covering for the given code. Can some one help to coverage for the code?

Apex Code>>>>>>>>>

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 Coverage>>>>>>>>>>>

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>();
        Map<Id,User> Mu = new Map<Id,User>();
        Mu.put(, u);
        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 = u.Id);
            insert psl;  
        u.IsActive= false;
        update u;
      //  HandlerClass_User hc = new HandlerClass_User();
         System.assertEquals(psl.isEmpty(), true);

SwethaSwetha (Salesforce Developers) 
HI Sriram,
I tried this code in my Dev org and see 0% coverage. To identify what went wrong you should add system.debug() statements in your test class.

I see the error as "List has no rows for assignment to SObject" at lines 6 and 7.

Ensure the Profile 'sys Admin' and the role 'Manager' exists in your org to fix the error. 

See related

If this information helps, please mark the answer as best. Thank you
SwethaSwetha (Salesforce Developers) 
Please also consider posting an image with which lines of code are not covered so the community can suggest better