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
Usha Charles 3Usha Charles 3 

Validation - Account Creation

Hello,

I am trying to create a validation on Account creation using a trigger (Just to learn APEX). I want only the users with Locale US/UK to be able to create accounts in UK/US & block the others. below is my code. but is dosent seem to be working as I get the error message for all users not just the ones with locale UK/US.

Can Someone please tell me what am I missing here? 

Thanks,
trigger ValidateAccount2 on Account (Before Insert, Before Update) {
    
    String UserLocale = userInfo.getLocale();
       For(Account acc : Trigger.new){
        If (acc.Country__c == ('UK') || acc.country__c =='US' && UserLocale != ('English - United States')){
            acc.addError('You are not allowed to add Accounts to other Regions. Please contact your Sales Admin');
            
        }
    }
}

 
Best Answer chosen by Usha Charles 3
Akhil AnilAkhil Anil
Hi Usha.

In the backend the locale values are stored as en_US. So your code will be like this
'
trigger ValidateAccount2 on Account (Before Insert, Before Update) {
    
    String UserLocale = userInfo.getLocale();
       For(Account acc : Trigger.new){
        If ((acc.Country__c == 'UK' || acc.country__c == 'US') && UserLocale != 'en_US'){
            acc.addError('You are not allowed to add Accounts to other Regions. Please contact your Sales Admin');           
        }
    }
}

 

All Answers

Akhil AnilAkhil Anil
Hi Usha.

In the backend the locale values are stored as en_US. So your code will be like this
'
trigger ValidateAccount2 on Account (Before Insert, Before Update) {
    
    String UserLocale = userInfo.getLocale();
       For(Account acc : Trigger.new){
        If ((acc.Country__c == 'UK' || acc.country__c == 'US') && UserLocale != 'en_US'){
            acc.addError('You are not allowed to add Accounts to other Regions. Please contact your Sales Admin');           
        }
    }
}

 
This was selected as the best answer
Usha Charles 3Usha Charles 3
Hello Akhil !

Thanks a lot ! that solved the issue !