In which condition we use setPreference method? Why?

David156
Giga Contributor

Any example of setPrefernce() method?

Any help will be appreciated.

Thanks

5 REPLIES 5

Mark Roethof
Tera Patron
Tera Patron

Hi there,

"There are situations where you may want to alter a given user preference as the result of a script running. For example, the Make this my current update set UI Action on Update Set records changes the user preference relating to the user's currently selected update set to the sys_id of the currently opened update set record. Luckily, ServiceNow provides both a client, and server-side API for performing this update.

It is possible to use a client script, business rule, or any client or server-side script to modify a user preference record, by calling one of the following APIs, depending on context:"

Client

setPreference('preference_name', 'preferece_value');

Server

gs.getUser().setPreference('pref_name', 'pref_value');

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

DirkRedeker
Mega Sage

Hi

ServiceNow supplies a number of OOB Preferences, but you can also create your on ones, which may be helpful for scripts.

You could for example not only store preferences, but also any kind of your user specific custom settings or values, that you like to store.

Be creative to control what you want to control. This just is limited to your creativity.

Let me know if that answered your question and mark my answer as correct and helpful

BR Dirk

Ganesh Pardhe
Kilo Guru

Hello There,

 

Setting preferences

how to set our own preferences. As an example, we will see how to change the user agent of your browser. These days, many web applications have a main site as well as a mobile site/m. site. The application will validate the user agent of the incoming request and decide whether to act as a server for a normal site or mobile site. So, in order to test your mobile site from your laptop or desktop browser, you just have to change your user agent. Let's see a code example where we can change the user-agent preference of our Firefox browser using FirefoxDriver, and send a request to the Facebook homepage. But before that, let's see the setPreference() method provided by the FirefoxProfile class:

public void setPreference(java.lang.String key, String value)

The input parameters are key, which is a string and represents your preference, and value, which has to be set to the preference.

 

There are two other overloaded versions of the preceding method shown; one of which is as follows:

public void setPreference(java.lang.String key, int value)

Here is the other overloaded version:

public void setPreference(java.lang.String key,boolean value)

Now, using the preceding setPreference() method, we will try to change the user agent of our browser using the following code:

public class SettingPreferences {
public static void main(String... args) {

System.setProperty("webdriver.gecko.driver",
"./src/test/resources/drivers/geckodriver 2");

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("general.useragent.override",
"Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) " +

"AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 " +
"Mobile/15A356 Safari/604.1");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(profile);
FirefoxDriver driver = new FirefoxDriver(firefoxOptions);
driver.get("http://facebook.com");
}
}

In the preceding code for the setPreference() method, general.useragent.override is set as the name of the preference, and the second parameter is the value for that preference, which represents the iPhone user agent. Now open the user.js file for this particular Firefox instance, and you will see the entry for this preference. You should use the following preference in your user.js file:

user_pref("general.useragent.override", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebK

 

If my answer is worth to you kindly mark correct and helpful.

 

regards,

Ganesh

 

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Did this solve your question? Or do we need to follow-up on this?

Please mark this answer as correct if it solves your question. This will help others who are looking for a similar solution. Also marking this answer as correct takes the post of the unsolved list.
Thanks.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn