Skip to content

Commit 5b7dbe6

Browse files
Merge branch 'main' into code-snippets
2 parents 177fcba + 09c2b13 commit 5b7dbe6

File tree

44 files changed

+1090
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1090
-0
lines changed

Attachments/CSVParser/script.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/********************************************************************************/
2+
Input:
3+
attachmentQuery - Pass sysId CSV attachment
4+
5+
Output:
6+
converted into object from CSV
7+
8+
/********************************************************************************/
9+
10+
function parseCSVFile(sysId) {
11+
var attachmentRecord = new GlideRecord("sys_attachment");
12+
attachmentRecord.get(sysId);
13+
attachmentRecord.query();
14+
15+
if (attachmentRecord.next()) {
16+
var stringUtil = new GlideStringUtil();
17+
var sysAttachment = new GlideSysAttachment();
18+
var bytesData = sysAttachment.getBytes(attachmentRecord);
19+
var encData = stringUtil.base64Encode(bytesData);
20+
var decData = stringUtil.base64Decode(encData) + '';
21+
22+
var delimiter = ',';
23+
var quoteCharacter = '"';
24+
var csvArray = decData.split("\r\n");
25+
26+
var index = 0
27+
var result = [];
28+
for (index = 0; index < csvArray.length; index++) {
29+
var row = csvArray[index];
30+
if (row) {
31+
var csvParser = new sn_impex.CSVParser().parseLineToArray(row, delimiter, quoteCharacter);
32+
var rowObject = {};
33+
for (var i = 0; i < csvParser.length; i++) {
34+
rowObject['column' + (i + 1)] = csvParser[i];
35+
}
36+
result.push(rowObject);
37+
}
38+
}
39+
return result;
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
3+
/**
4+
* Deletes records from multiple tables based on provided encoded queries.
5+
*
6+
* @param {Object} target - An object where each key is the name of a table and each value is an encoded query string.
7+
* The function will delete all records matching the encoded query for each specified table.
8+
*
9+
* Example usage:
10+
* bulkDelete({
11+
* 'incident': 'priority=1^state=2',
12+
* 'change_request': 'state=3^risk=high'
13+
* });
14+
*
15+
* This deletes all records in the 'incident' table where the priority is 1 and the state is 2,
16+
* and all records in the 'change_request' table where the state is 3 and risk is 'high'.
17+
*/
18+
function bulkDelete(target) {
19+
20+
for (var table in target) {
21+
if (target.hasOwnProperty(table)) {
22+
var getRecord = new GlideRecord(table);
23+
getRecord.addEncodedQuery(target[table]);
24+
getRecord.query();
25+
while (getRecord.next()) {
26+
27+
getRecord.deleteRecord();
28+
}
29+
}
30+
}
31+
}
32+
33+
34+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Bulk Delete Function Documentation - Use the code/function to bulk-deletes records from multiple tables based on provided encoded queries.
2+
3+
# Function: `bulkDelete(target)`
4+
5+
Deletes records from multiple tables based on provided encoded queries.
6+
7+
## Parameters
8+
9+
- **`target`** (`Object`): An object where each key is the name of a table, and each value is an encoded query string.
10+
- The function will delete all records matching the encoded query for each specified table.
11+
12+
## Example Usage
13+
14+
```javascript
15+
bulkDelete({
16+
'incident': 'priority=1^state=2',
17+
'change_request': 'state=3^risk=high'
18+
});
19+
```
20+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
3+
/**
4+
* Performs a bulk update on a specified table, applying the given data to all records that match the query.
5+
*
6+
* @param {string} table - The name of the table where the bulk update is to be performed.
7+
* @param {string} query - The encoded query string that filters which records to update.
8+
* @param {Object} data - An object representing the field-value pairs to update.
9+
* Each key is a field name, and the value is the new value for that field.
10+
*
11+
* Example usage:
12+
* bulkUpdate('incident', 'priority=1^state=2', { priority: 2, state: 3 });
13+
*
14+
* This updates all incidents where priority is 1 and state is 2, setting priority to 2 and state to 3.
15+
*/
16+
function bulkUpdate(table, query, data) {
17+
18+
var getRecord = new GlideRecord(table);
19+
getRecord.addEncodedQuery(query);
20+
getRecord.query();
21+
while (getRecord.next()) {
22+
for (var field in data) {
23+
if (data.hasOwnProperty(field)) {
24+
getRecord.setValue(field, data[field]);
25+
}
26+
}
27+
getRecord.update();
28+
}
29+
}
30+
31+
32+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Bulk Update Function Documentation - Use the code/function to bulk change some fields in any tables.
2+
3+
## `bulkUpdate(table, query, data)`
4+
5+
Performs a bulk update on a specified table, applying the given data to all records that match the query.
6+
7+
### Parameters
8+
9+
- **`table`** (`string`): The name of the table where the bulk update is to be performed.
10+
- **`query`** (`string`): The encoded query string that filters which records to update.
11+
- **`data`** (`Object`): An object representing the field-value pairs to update.
12+
- Each key is a field name, and the value is the new value for that field.
13+
14+
### Example Usage
15+
16+
```javascript
17+
bulkUpdate('incident', 'priority=1^state=2', { priority: 2, state: 3 });
18+
```
19+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# update a record in sys_user table
2+
3+
If we use the command below to update a record, it can lead to a problem.
4+
5+
grUser.get('grUser.get('62826bf03710200044e0bfc8bcbe5df9')');
6+
7+
If the record is not found in the table, the script will create a new one.
8+
9+
To make sure we are updating and not inserting, it is better to wrap up the get method with an If statement.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var grUser = new GlideRecord('sys_user');
2+
3+
if (grUser.get('62826bf03710200044e0bfc8bcbe5df9')) {
4+
grUser.user_name = 'test.user';
5+
grUser.first_name = 'test';
6+
grUser.last_name = 'user';
7+
grUser.email = 'test.user@servicenow';
8+
grUser.update();
9+
} else {
10+
gs.info('Record not found.');
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function onLoad() {
2+
var mgr = g_service_catalog.parent.getValue('v_manager'); //Catalog Item Variable Name
3+
var filter = g_list.get('v_employee'); //MRVS variable name
4+
filter.setQuery('active=true^manager=' + mgr);
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*** This solution uses g_list, so it works in Service Portal (and I assume ESC) only, not the native UI / Service Catalog interface ***<br>
2+
Developed in collaboration with Chris Perry
3+
4+
5+
On a reference variable in a multi-row variable set, sometimes you want the reference qualifier to include the value of a variable that is part of the Catalog Item, not within the MRVS
6+
7+
When using this script that applies to the Variable set, not the Catalog Item, leave the Reference qualifier field on the MRVS variable empty
8+
9+
In this simplified example, I have a Manager (v_manager) reference variable (sys_user table) that belongs to the Catalog Item. In the MRVS, I have an Employee (v_employee) reference variable (sys_user table). I only want to be able to select user records that are active, and the Manager is the user I selected on the Catalog Item variable.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function onLoad() {
2+
3+
var taskId = getParameterValue("taskid");
4+
5+
if (taskId != "" && taskId != null && taskId != undefined) {
6+
console.log('=== CAMACHO Task id: ' + taskId);
7+
8+
var gaGetTaskNumber = new GlideAjax('UtilsAjax');
9+
gaGetTaskNumber.addParam('sysparm_name', 'getTaskNumber');
10+
gaGetTaskNumber.addParam('sysparm_task_id', taskId);
11+
gaGetTaskNumber.getXMLAnswer(setmyFormValue);
12+
}
13+
}
14+
15+
function setmyFormValue(answer) {
16+
17+
//console.log('=== CAMACHO Entered setmyFormValue');
18+
if (answer) {
19+
var obj = JSON.parse(answer);
20+
var numero = obj.number.toString();
21+
console.log(numero);
22+
23+
g_form.setValue('task_number', numero);
24+
25+
}
26+
}
27+
28+
function getParameterValue(name) {
29+
var url = top.location.href;
30+
var value = new URLSearchParams(url).get(name);
31+
if (value) {
32+
return value;
33+
} else {
34+
return null;
35+
}
36+
}

0 commit comments

Comments
 (0)