1

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.

3
  • Same code i have tried in dev org.. Its working as expected and i am not getting any error when updating details through my personal information section. Commented May 23, 2022 at 8:53
  • Thanks for checking, Can you please first disable the trigger & make the country blank. Then enable the trigger & try to update the records with the country having value. Commented May 24, 2022 at 3:35
  • 1
    Yes you are correct, when disable the trigger and keep the country value null and then enabled the trigger and keep the country value upon save getting error and checked in the advanced user details country value got updated. Commented May 24, 2022 at 4:07

1 Answer 1

0

The code works fine for me. Check if any recursion happening.

Trigger

trigger User on User (before insert, before update) {
   if(trigger.Isbefore){
      usertest.onBeforeUpdate(trigger.new);
   }
}
public  class usertest {
   public static void  onBeforeUpdate(List<User> newUsers) {      
      for ( User user : newUsers){        
         system.debug('country =' + user.Country);
         if (user.Country == null || user.Country == '' ) {
            user.adderror('Country field is required, Please enter country & try again.');
         }
      } 
   }
}
1
  • Thanks for checking, Can you please first disable the trigger & make the country blank. Then enable the trigger & try to update the records with the country having value. Commented May 24, 2022 at 3:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .