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

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

 

Hi, I have used this script in Virtual agent script but without logging out the user I am not able to see the changes. Any idea about this ?

AnirudhKumar
Mega Sage
Mega Sage

Did you get a chance to try the solution?

Hello @AnirudhKumar !

Yes, it works like a charm, perfect!

Thank you so much for your help, highly appreciated 🙂

Kindest regards,

Lili