+ Start a Discussion
Sourav PSourav P 

How to block an user from changing Time zone & Locale in his profile setting?

Hi,
I want a certain profile users to block changing the Time zone and Locale field on their below set up, Can anybody plz suggest me to write a trigger to do so?
User-added image
 
Best Answer chosen by Sourav P
Raj VakatiRaj Vakati
Please use this trigger and modify it based on the profile names 
 
trigger userPermSetAssignment on User (before update) 
{ 
    Map<Id,User> oldUsserMap = Trigger.oldMap ;
    
    Profile p = [Select Id, Name from Profile where Name = 'Tier 1 Agent'];


    for(User u : Trigger.new){
    If(u.ProfileId ==p.Id){
        if(u.LocaleSidKey!=oldUsserMap.get(u.Id).LocaleSidKey){
            u.addError('YOU CANNT ABLE TO CHANGE THE TIME ZONE ');
        }
         if(u.TimeZoneSidKey!=oldUsserMap.get(u.Id).TimeZoneSidKey){
            u.addError('YOU CANNT ABLE TO CHANGE THE TIME ZONE ');
        }
        }
    }     
    
}

 

All Answers

Raj VakatiRaj Vakati
Please use this trigger and modify it based on the profile names 
 
trigger userPermSetAssignment on User (before update) 
{ 
    Map<Id,User> oldUsserMap = Trigger.oldMap ;
    
    Profile p = [Select Id, Name from Profile where Name = 'Tier 1 Agent'];


    for(User u : Trigger.new){
    If(u.ProfileId ==p.Id){
        if(u.LocaleSidKey!=oldUsserMap.get(u.Id).LocaleSidKey){
            u.addError('YOU CANNT ABLE TO CHANGE THE TIME ZONE ');
        }
         if(u.TimeZoneSidKey!=oldUsserMap.get(u.Id).TimeZoneSidKey){
            u.addError('YOU CANNT ABLE TO CHANGE THE TIME ZONE ');
        }
        }
    }     
    
}

 
This was selected as the best answer
Sourav PSourav P
Hi Raj, Thanks a lot. I followed the trigger and its working perfect.
As a beginner to apex, Can you also plz suggest to write the test class to move to production.
 
Raj VakatiRaj Vakati
try this
 
@isTest
private class UserTriggerTest{
    
    static testMethod void insertUserTest(){
         Profile pf= [Select Id from profile where Name='YOUR_PROFILE']; 
        
        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 = uniqueName + '@test' + orgId + '.org', 
                         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;
		
		uu.TimeZoneSidKey=Australia/Lord_Howe';
		update uu ;
    }
   
}

 
Raj VakatiRaj Vakati
working ???
Sourav PSourav P
Hi Raj, Thanks a lot.
I was testing the code coverage. I will keep the Name ABC, & XYZ as it is? No need to take a real name from the org ?  Or should be real name from the org ?
Also, if you can brief me why we have taken below for my understanding plz, what we did actually in teh test class, and what we checked here
String orgId=UserInfo.getOrganizationId(); 
08
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
09
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
10
        String uniqueName=orgId+dateString+RandomId;


 
Raj VakatiRaj Vakati
Its up to you ...  you can keep real name or you keep some dummy name as its test class

If it is solved your problem close this thread 
 
Raj VakatiRaj Vakati
In this code i am generating the uniquer user in test class so that your test class wnr fail if the same username is is aready in use ..


        String uniqueName=orgId+dateString+RandomId;
Sourav PSourav P
Hi raj, 
Also showing a code coverage issue, as below
Code Coverage Warning
Test coverage of active processes and autolaunched flows is 0%, but at least 75% is required.
Method name: insertUserTest
Error message : System.DmlException: Update failed. First exception on row 0 with id 0050k000002T6r2AAC; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, YOU CAN NOT ABLE TO CHANGE THE TIME ZONE: [] 
Stack Trace: Class.UsertimezoneRestrictionTest.insertUserTest: line 27, column 1



 
Raj VakatiRaj Vakati
Comment this test two lines and try 

uu.TimeZoneSidKey=Australia/Lord_Howe'; update uu ;
Sourav PSourav P
Hi Raj, After commenting that two lines, i can till see coverage error, But different from last time , now it changed as below
 
Code Coverage Warning
Your code coverage is 0%. You need at least 75% coverage to complete this deployment.
UsertimezoneRestriction
Test coverage of active processes and autolaunched flows is 0%, but at least 75% is required.

 
Sourav PSourav P
UsertimezoneRestriction, is teh trigger name i have given
Sourav PSourav P
all same in my test class except below, 
 /*uu.TimeZoneSidKey='Australia/Lord_Howe';
        update uu ;*/
Sourav PSourav P
trigger UsertimezoneRestriction on User (before update) {
 
Sourav PSourav P
Hi Raj, Can you plz suggest, if we can change anything in the current test class to cover atleast 75%
Sourav PSourav P
Hi Raj, Thanks a lot. Now i am able to manage with the test class.