The CreatorCon Call for Content is officially open! Get started here.

How to update User Preferences 'Language' setting in the Service Portal when the user profile's preferred_language changes?

Melinda7
Kilo Expert

Hello everyone!

I am quite new to scripting and now I got a requirement to create a business rule in order to update the User Preferences 'Language' setting on the Service Portal, whenever the user changes his preferred_language in his/her user profile (backend).

The other way round, I mean changing the language in the SP and having it synchronized with the user profile preferred_language in the system is now working thanks to this BR (please suggest any improvements, I would be thankful for it):

(function executeRule(current, previous /*null when async*/ ) {

    grUser = new GlideRecord('sys_user');
    grUser.addQuery('sys_id', current.user);
    grUser.query();
    if (grUser.next()) {
        grUser.preferred_language = current.value;
        grUser.update();
    }
})(current, previous);

BUT, as mentioned above, I need this to work vice-versa.

In the backend, when changing the:

- user profile preferred_language

 

=> this shall update the user preference language in the frontend, too.

 

ADDITIONAL requirement:

Also, this new business rule shall first check when change to the preferred_language is made, whether the language of the SP is the same as this newly changed user profile preferred_language.

If it is the same, it shall not update it. If it is different, it shall update it in the portal to the new language.

 

Very thankful in advance for any help or advice. 

1 ACCEPTED SOLUTION

AnirudhKumar
Mega Sage

I hope I'm not too late...

Here's the solution:

Write a before update Business Rule on sys_user table with Filter Conditions = Language Changes

Script:

(function executeRule(current, previous /*null when async*/ ) {


    var userPref = new GlideRecord("sys_user_preference");
    userPref.addQuery("user", gs.getUserID());
    userPref.addQuery("name", 'user.language');
    userPref.query();
    if (userPref.next()) {
        if (userPref.value != current.preferred_language) {
            
			//Deleting the existing user Preference
			userPref.deleteRecord();

			//Creating new User preference
            var userPref2 = new GlideRecord("sys_user_preference");
            userPref2.initialize();
			userPref2.name = 'user.language';
			userPref2.user = gs.getUserID();
			userPref2.value = current.preferred_language;
			userPref2.insert();
        }
    }





})(current, previous);

 

View solution in original post

12 REPLIES 12

Linda,

Any logged in user should be able to change their language (which is the value in [sys_user.preferred_language] field, which by default changes the language of that user's session preference).

It sometimes means that the user needs to do a full page refresh so as to get the browser to download all the strings again back in the changed language but this is how the ootb behaviour should be,

The options of a user changing their language can be found here:
https://docs.servicenow.com/bundle/quebec-platform-administration/page/administer/localization/reference/r_UserSpecificLanguage.html

Adding a customisation for someone to change someone else's language preference would not be recommended as it is essentially increasing the technical debt due to a custom script, when the same can be achieved by adding the "language switcher" widget to the main portal page (which achieves the same outcome of an end-user being able to easily change their language in the portal);
https://docs.servicenow.com/bundle/quebec-servicenow-platform/page/build/service-portal/concept/language-switch-widget.html


Many thanks,
Kind regards

--------------------------------------------------------------------
Director of Globalization Deployment, Internationalization

Hi @Alex Coope ,

thank you! I totally agree with you and earlier today I actually informed our client about the same. It doesn't seem best practice to neither from my own team to provide the user a possibility to change other user's language preference, unless that user is an admin, which is okay.

Thank you once again for all the detailed explanation.

Kindest regards,

Linda

Linda, 

By all means reach out to me by email if you need assistance with the Customer,

Many thanks,
Kind regards

--------------------------------------------------------------------
Director of Globalization Deployment, Internationalization