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
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

Dear @AnirudhKumar could you maybe advise about how to modify this script in case it needs to cover the possibility of signed in user changing the sys_user.preferred_language setting of any other user in the sys_user table?

a) so that me, if I am the signed-in user and change MY OWN sys_user.preferred_language setting, it also changes the language setting of the service portal, 

but also

b) me, as a signed-in user, when I change SOMEBODY else's sys_user.preferred_language setting, it changes THAT PERSON'S language setting of his/her service portal

Can you, pretty please, advise me?

Here's the script you are looking for:

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


    var userPref = new GlideRecord("sys_user_preference");
    userPref.addQuery("user", current.sys_id);
    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 = current.sys_id;
            userPref2.value = current.preferred_language;
            userPref2.insert();
        }
    } else {//If user does not already have a preference, create one
        //Creating new User preference
        var userPref2 = new GlideRecord("sys_user_preference");
        userPref2.initialize();
        userPref2.name = 'user.language';
        userPref2.user = current.sys_id;
        userPref2.value = current.preferred_language;
        userPref2.insert();
    }





})(current, previous);

 

Edited to add else block so that when you change somebody's preference, and they do not have the preference already, system creates one

Hi @AnirudhKumar 

Unfortunately, if I change someone else's language preference in the sys_user.preferred_language, it does not reflect in the Service Portal.

It only works when I change my own language preference 😞

However, this opens up a nice possibility to change somebody's instance language to Japanese and play a prank on the victim.

@AnirudhKumar 

I thought absolutely the same 😄

Generally, a standard end-user does not have access to the sys_user table to change their language preference, but hey, if this is what my client wants, I need to implement it 🙂

Thank you loads once again!