Skip to content

Commit f0c0aab

Browse files
authored
UX Data Broker Transform Create/Update User Preference (#1574)
* Create create-update-user-preference.js Added the code for a transform data broker that creates or updates the current user's sys_user_preference * Create readme.md
1 parent 5014a5f commit f0c0aab

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* This data broker creates or updates a user preference of the given name with the given value
3+
* @param {{name: string, value?: string}} inputs inputs from the properties field above
4+
*/
5+
function transform({ name, value }) {
6+
const lib = "Data Broker";
7+
const func = "Create/Update User Preference";
8+
try {
9+
if (!name) throw new Error("Missing required param 'name'");
10+
11+
12+
gs.getUser().savePreference(name, value);
13+
} catch (e) {
14+
gs.error(`${lib} ${func} - ${e}`);
15+
throw new Error(e);
16+
}
17+
}
18+
19+
/**
20+
* Make sure to select that this data broker mutates data
21+
* Input this in the properties field:
22+
*
23+
[
24+
{
25+
"name": "name",
26+
"label": "Name",
27+
"description": "The name of the user preference to create or update",
28+
"readOnly": false,
29+
"fieldType": "string",
30+
"mandatory": true,
31+
"defaultValue": ""
32+
},
33+
{
34+
"name": "value",
35+
"label": "Value",
36+
"description": "The value to store in the user preference",
37+
"readOnly": false,
38+
"fieldType": "string",
39+
"mandatory": false,
40+
"defaultValue": ""
41+
}
42+
]
43+
*/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Create/Update User Preference Data Broker
2+
3+
This repository provides a data broker for creating or updating user preferences in ServiceNow. The broker allows you to specify a preference name and value, storing or updating it in the user’s preferences.
4+
5+
## Features
6+
7+
- **Create or Update User Preferences**: Provides a simple interface for adding or modifying preferences by name and value.
8+
- **Error Handling**: Logs errors if the preference name is missing or if an unexpected issue arises.
9+
10+
## Template Overview
11+
12+
```javascript
13+
/**
14+
* This data broker creates or updates a user preference of the given name with the given value
15+
* @param {{name: string, value?: string}} inputs inputs from the properties field above
16+
*/
17+
function transform({ name, value }) {
18+
const lib = "Data Broker";
19+
const func = "Create Update User Preference";
20+
try {
21+
if (!name) throw new Error("Missing required param 'name'");
22+
23+
gs.getUser().savePreference(name, value);
24+
} catch (e) {
25+
gs.error(`${lib} ${func} - ${e}`);
26+
throw new Error(e);
27+
}
28+
}
29+
```

0 commit comments

Comments
 (0)