I have added country validation in the user trigger so when the country is blank then the system displays validation error to the user. My trigger code is working as expected when we modify the user data from setup -> user but when I try to modify from personal information (Go to-> My setting -> Personal -> click on "Personal Information" ) then the system throws validation error even country is not blank. I added system debug in trigger code & found country & countryCode both fields are null. This issue is only when users modify records from personal settings.
public void onBeforeUpdate(List<User> newUsers) {
for ( User user : newUsers ){
system.debig('country =' + user.Country);
if (user.Country == null || user.Country == '' ) {
user.adderror('Country field is required, Please enter country & try again.');
}
}
Just to update you one more thing, the system fires trigger two times when updating the data from personal information. the first time the country is blank & for the second time it's having the correct value as per debug log. So even we don't understand this behavior of salesforce.