Skip to content

Commit 7ef352c

Browse files
Merge branch 'main' into SNippets2025
2 parents 65c5afe + 036d425 commit 7ef352c

File tree

220 files changed

+8286
-480
lines changed

Some content is hidden

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

220 files changed

+8286
-480
lines changed

.github/pull_request_template.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# PR Description:
2-
2+
replace this with your description
33

44
# Pull Request Checklist
55

66
## Overview
7+
- [x] Put an x inside of the square brackets to check each item.
78
- [ ] I have read and understood the [CONTRIBUTING.md](CONTRIBUTING.md) guidelines
8-
- [ ] My pull request has a descriptive title that accurately reflects the changes
9+
- [ ] My pull request has a descriptive title that accurately reflects the changes and the description has been filled in above.
910
- [ ] I've included only files relevant to the changes described in the PR title and description
1011
- [ ] I've created a new branch in my forked repository for this contribution
1112

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
This piece of code is designed for an usecase where you might want to populate a field value that you're passing as a query in the URL which redirects to a catalog item.
2+
In this case, a custom field 'u_date' is chosen as an example to be shown:
3+
4+
1. You open a catalog item record via a URL that carries a date in the query string.
5+
Example:
6+
https://your-instance.service-now.com/your_form.do?sysparm_u_date=2025-10-31
7+
-(This URL includes a parameter named sysparm_u_date with the value 2025-10-31.)
8+
9+
10+
2. The catalog client script reads the page URL and extracts that specific parameter which returns the value "2025-10-31".
11+
12+
3. If the parameter is present, the script populates the form field.
13+
Calling g_form.setValue('u_date', '2025-10-31') sets the date field on the form to 31 October 2025.
14+
15+
16+
Result:
17+
The date field in the form is prefilled from the URL
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//Logic to fetch the u_date field value passed in the url and setting it in the actual field.
2+
3+
4+
var fetchUrl = top.location.href; //get the URL
5+
6+
var setDate = new URLSearchParams(gUrl).get("sysparm_u_date"); //fetch the value of date from the query parameter
7+
8+
g_form.setValue('u_date', setDate); //set the value to the actual field
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
# Clear all fields on a catalog item form
22

3-
This works on both the native platform and service portal / mobile. Typically used with an OnChange catalog client script when you would like to reset all the fields after a certain variable is changed.
3+
This function clears all editable fields on a form, except those explicitly excluded.
4+
It works on both the native platform (Classic UI) and Service Portal / Mobile.
5+
Typically used with an OnChange catalog client script when you want to clear all fields after a certain variable changes.
46

5-
This function does support an exclusion list if there are fields you would like to exclude from being reset, typically you would want to add the field that triggered to the change to the exlusion
7+
The function returns an array of the field names that were cleared, which can be used for logging or further processing.
68

7-
### Example
9+
### Exclusion Support
810

9-
```js
10-
clearFields(['field1', 'field2']);
11-
```
11+
You can pass an array of field names to exclude from being cleared.
12+
This is useful when you want to preserve the value of the field that triggered the change or other important fields.
1213

13-
All fields on the form **except** field1 and field2 will be cleared.
14+
### Example
15+
```
16+
clearFields(['short_description', 'priority']);
17+
```
18+
// Clears all fields except 'short_description' and 'priority'
Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,47 @@
1-
/**SNDOC
2-
@name clearFields
3-
@description Clear/reset all fields on a form
4-
@param {Array} [dontClearFieldsArray] - Fields to not clear
5-
@example
6-
clearFields(['field1', 'field2']);
7-
*/
1+
/**
2+
* Clears or resets all editable fields on a form, except those explicitly excluded.
3+
* Compatible with Classic UI and Service Portal/Mobile.
4+
* Intended for use in onChange client scripts.
5+
*
6+
* @function clearFields
7+
* @param {Array} dontClearFieldsArray - Array of field names to exclude from clearing.
8+
* @returns {Array} - Array of field names that were cleared.
9+
*
10+
* @example
11+
* // Clears all fields except 'short_description' and 'priority'
12+
* clearFields(['short_description', 'priority']);
13+
*/
14+
function clearFields(dontClearFieldsArray) {
15+
// Ensure the exclusion list is defined and is an array
16+
dontClearFieldsArray = Array.isArray(dontClearFieldsArray) ? dontClearFieldsArray : [];
817

9-
function clearFields(dontClearFieldsArray){
18+
// Helper function to check if a field should be cleared
19+
function shouldClear(fieldName) {
20+
return dontClearFieldsArray.indexOf(fieldName) === -1;
21+
}
1022

11-
try{ // Classic UI
12-
var pFields = g_form.nameMap;
13-
pFields.forEach(function(field){
14-
if(dontClearFieldsArray.indexOf(field.prettyName) == -1){
15-
g_form.clearValue(field.prettyName);
16-
}
17-
});
18-
}catch(e){ // Service Portal or Mobile
19-
var fields = g_form.getEditableFields();
20-
fields.forEach(function(field){
21-
if(dontClearFieldsArray.indexOf(fields) == -1){
22-
g_form.clearValue(field);
23-
}
24-
});
25-
}
26-
}
23+
var clearedFields = [];
24+
25+
try {
26+
// Classic UI: use g_form.nameMap to get all fields
27+
var allFields = g_form.nameMap;
28+
allFields.forEach(function(field) {
29+
var fieldName = field.prettyName;
30+
if (shouldClear(fieldName)) {
31+
g_form.clearValue(fieldName);
32+
clearedFields.push(fieldName);
33+
}
34+
});
35+
} catch (e) {
36+
// Service Portal or Mobile: use getEditableFields()
37+
var editableFields = g_form.getEditableFields();
38+
editableFields.forEach(function(fieldName) {
39+
if (shouldClear(fieldName)) {
40+
g_form.clearValue(fieldName);
41+
clearedFields.push(fieldName);
42+
}
43+
});
44+
}
45+
46+
return clearedFields;
47+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var AccountUtils = Class.create();
2+
AccountUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
3+
4+
//Populate the department name from the account in the session data for the reference qualifier to use:
5+
6+
setSessionData: function() {
7+
var acct = this.getParameter('sysparm_account');
8+
var dept = '';
9+
var acctGR = new GlideRecord('customer_account'); //reference table for Account variable
10+
if (acctGR.get(acct)) {
11+
dept = '^dept_name=' + acctGR.dept_name; //department field name on account table
12+
}
13+
14+
var session = gs.getSession().putClientData('selected_dept', dept);
15+
return;
16+
},
17+
18+
type: 'AccountUtils'
19+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
This Catalog Client Script and Script Include are used with a reference qualifier similar to
2+
javascript: 'disable=false' + session.getClientData('selected_dept');
3+
4+
The scenario is a MRVS with a reference variable to the customer account table. When an (active) account is selected in the first row, subsequent rows should only be able to select active accounts in the same department as the first account.
5+
6+
The Catalog Client Script will pass the first selected account (if there is one) to a Script Include each time a MRVS row is added or edited. The Script Include will pass the department name to the reference qualifier.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function onLoad() {
2+
//applies to MRVS, not Catalog Item. This will pass the first selected account (if there is one) to a Script Include each time a MRVS row is added or edited
3+
var mrvs = g_service_catalog.parent.getValue('my_mrvs'); //MRVS internal name
4+
var acct = '';
5+
if (mrvs.length > 2) { //MRVS is not empty
6+
var obj = JSON.parse(mrvs);
7+
acct = obj[0].account_mrvs;
8+
}
9+
var ga = new GlideAjax('AccountUtils');
10+
ga.addParam('sysparm_name', 'setSessionData');
11+
ga.addParam('sysparm_account', acct);
12+
ga.getXMLAnswer(getResponse);
13+
}
14+
15+
function getResponse(response) {
16+
//do nothing
17+
}

Client-Side Components/Catalog Client Script/Regex Validation/README.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

Client-Side Components/Catalog Client Script/Regex Validation/script.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)