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
Nishigandha JadhavNishigandha Jadhav 

not covering add error method inside if statement

trigger usertrigger on User (before update) {
//Set<ID> username = new Set<ID>{'0050z000007XtMB'};
   
  
   
   
 //Map<ID,User> usernames = new Map<ID,User>([SELECT ID, Name FROM User WHERE Name IN :username]);
  Map<ID,User> usernames = new Map<ID,User>([SELECT ID, Name FROM User WHERE ID in ('0050z000007XtMB' ,'00560000004Mpmp') ]);//(Checking for user id)
    for(User o:trigger.new){
        if(Trigger.oldMap.get(o.id).email!=o.email && usernames.containsKey(o.id) ){
        o.email.addError('you cannot edit the email');
        }
        else if(Trigger.oldMap.get(o.id).username!=o.username && usernames.containsKey(o.id)){
            o.username.addError('you cannot change the username');
        }
        else if(Trigger.oldMap.get(o.id).isActive!=o.isActive && usernames.containsKey(o.id)){
            o.isActive.addError('You cannot deactivate the user');
           
        }
        else if(Trigger.oldMap.get(o.id).profileID !=o.profileID && usernames.containsKey(o.id) ){
            o.profileId.addError('you cannot edit the profile');}
    }
}
Best Answer chosen by Nishigandha Jadhav
Raj VakatiRaj Vakati
First Remove the hardcoded user id from the trigger and try something like below 
 
trigger usertrigger on User (before update) {
//Set<ID> username = new Set<ID>{'0050z000007XtMB'};
 //Map<ID,User> usernames = new Map<ID,User>([SELECT ID, Name FROM User WHERE Name IN :username]);
  Map<ID,User> usernames = new Map<ID,User>([SELECT ID, Name FROM User WHERE Email in ('test@gmail.com' ,'test@gmail.com') ]);//(Checking for user id)
    for(User o:trigger.new){
        if(Trigger.oldMap.get(o.id).email!=o.email && usernames.containsKey(o.id) ){
        o.email.addError('you cannot edit the email');
        }
        else if(Trigger.oldMap.get(o.id).username!=o.username && usernames.containsKey(o.id)){
            o.username.addError('you cannot change the username');
        }
        else if(Trigger.oldMap.get(o.id).isActive!=o.isActive && usernames.containsKey(o.id)){
            o.isActive.addError('You cannot deactivate the user');
           
        }
        else if(Trigger.oldMap.get(o.id).profileID !=o.profileID && usernames.containsKey(o.id) ){
            o.profileId.addError('you cannot edit the profile');}
    }
}
 
@isTest
private class UserTriggerTest{
    @testSetup
    static void setupTestData(){
        
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = 'test@gmail', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        
    }
    static testMethod void test_postive (){
		try {
	User u = [Select Id,IsActive,Email,Username,Profile from User limit 1 ] ; 
	u.Email='Demo@gmail.com' ; 
	u.Username ='Test@gmail.com'; 
	u.IsActive = false ; 
	update u ; 
		}catch(Exception e){
		}

  }
  
}