Skip to content

Commit ebc61fa

Browse files
authored
Merge branch 'main' into hacktoberfest2025-restmessage-contribution
2 parents b24d047 + 6f1bdbf commit ebc61fa

File tree

537 files changed

+19595
-110
lines changed

Some content is hidden

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

537 files changed

+19595
-110
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var GetRecentRequestValues = Class.create();
2+
GetRecentRequestValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {
3+
getValues: function() {
4+
var userID = this.getParameter('sysparm_user');
5+
var itemID = this.getParameter('sysparm_item');
6+
var result = { found: false, values: {} };
7+
8+
var gr = new GlideRecord('sc_req_item');
9+
gr.addQuery('requested_for', userID);
10+
gr.addQuery('cat_item', itemID);
11+
gr.orderByDesc('sys_created_on');
12+
gr.setLimit(1);
13+
gr.query();
14+
15+
if (gr.next()) {
16+
result.found = true;
17+
18+
19+
var vars = gr.variables;
20+
result.values = {
21+
'requested_for': vars.requested_for + '',
22+
'location': vars.location + '',
23+
'department': vars.department + ''
24+
};
25+
}
26+
27+
return JSON.stringify(result);
28+
}
29+
});
30+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function onLoad() {
2+
var user = g_user.userID;
3+
var itemID = g_form.getUniqueValue();
4+
5+
var ga = new GlideAjax('GetRecentRequestValues');
6+
ga.addParam('sysparm_name', 'getValues');
7+
ga.addParam('sysparm_user', user);
8+
ga.addParam('sysparm_item', itemID);
9+
ga.getXMLAnswer(function(response) {
10+
var data = JSON.parse(response);
11+
if (data && data.found) {
12+
var confirmFill = confirm("We found a similar request. Do you want to autofill fields?");
13+
if (confirmFill) {
14+
for (var field in data.values) {
15+
if (g_form.getControl(field)) {
16+
g_form.setValue(field, data.values[field]);
17+
console.log("Set " + field + " to " + data.values[field]);
18+
} else {
19+
console.log("Field not found: " + field);
20+
}
21+
}
22+
}
23+
} else {
24+
console.log("No previous request found.");
25+
}
26+
});
27+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Recent Request Autofill for ServiceNow Catalog.it automatically offers to fill in fields based on the user's most recent similar request.
2+
Features
3+
- Detects previous requests for the same catalog item
4+
- Prompts user to reuse values from their last submission
5+
- Autofills fields like location, department, and justification
6+
7+
<img width="878" height="395" alt="image" src="https://github.com/user-attachments/assets/33ceabf5-2bbc-43e3-8792-f1f9a99699d2" />
8+
9+
10+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* The following is a client callable script include. This can be used with the onChange Client script to be able to gather the data on the server side
3+
*/
4+
5+
var ReferenceQualifierAjaxHelper = Class.create();
6+
ReferenceQualifierAjaxHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
7+
getUserInformation : function() {
8+
var userID = this.getParameter('sysparm_user');
9+
var userRec = new GlideRecord('sys_user');
10+
11+
if(userRec.get(userID)) {
12+
var results = {
13+
"email" : userRec.getValue('email'),
14+
"department" : userRec.getValue('department'),
15+
"title" : userRec.getValue('title'),
16+
"phone" : userRec.getValue('phone')
17+
};
18+
19+
return JSON.stringify(results)
20+
}
21+
22+
},
23+
24+
type: 'ReferenceQualifierAjaxHelper'
25+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Overview
2+
This onchange catalog client script and script inlcude work together autopopulate the user fields that might show up on a catalog item. In the
3+
global scope you will have to create the client callable script include to be able to use the Ajax call that is in the on change client script.
4+
In this example we use the OOB Requested For field that already auto populates the user that is logged in then we go to the server to get that
5+
users information. The fields that are brough back are the ones that are in the code but you can modify to bring back more or less fields if needed.
6+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* In order for this to work make sure to have an onChange catalog client script on a variable that is type Requested For. This variable
3+
* already autopopulates the logged in user with its OOB functionality. In the updateUserFields function you can add any other user fields
4+
* that you might need.
5+
*/
6+
7+
function onChange(control, oldValue, newValue, isLoading) {
8+
//This variable will store the sys_id of the user that populates in your requested for variable
9+
var userID = newValue;
10+
11+
var ga = new GlideAjax(ReferenceQualifierAjaxHelper);
12+
ga.addParam('sysparm_name', 'getUserInformation');
13+
ga.addParam('sysparm_user', userID);
14+
ga.getXMLAnswer(updateUserFields);
15+
16+
function updateUserFields(response) {
17+
var returnedData = JSON.parse(response);
18+
g_form.setValue("email", returnedData.email);
19+
g_form.setValue("department", returnedData.department);
20+
g_form.setValue("title", returnedData.title);
21+
g_form.setValue("phone", returnedData.phone);
22+
}
23+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This project adds a dynamic preview feature to Service Catalog items, allowing users to see the full approval chain before submitting a request. It improves transparency, reduces confusion, and helps users understand who will be involved in the approval process based on their selections.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function onLoad() {
2+
var ga = new GlideAjax('ApprovalChainHelper');
3+
ga.addParam('sysparm_name', 'getApprovers');
4+
ga.addParam('sysparm_item_id', g_form.getUniqueValue());
5+
ga.getXMLAnswer(function(response) {
6+
var approvers = JSON.parse(response);
7+
var message = 'This request will be approved by: ' + approvers.join(', ');
8+
g_form.showFieldMsg('requested_for', message, 'info');
9+
});
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var ApprovalChainHelper = Class.create();
2+
ApprovalChainHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
3+
getApprovers: function() {
4+
var itemId = this.getParameter('sysparm_item_id');
5+
var userId = gs.getUserID();
6+
7+
var approvers = [];
8+
9+
// Example logic: fetch approval rules based on item and user
10+
var ruleGR = new GlideRecord('sysapproval_approver');
11+
ruleGR.addQuery('document_id', 80f8920bc3e4b2105219daec050131e3);
12+
ruleGR.query();
13+
14+
while (ruleGR.next()) {
15+
approvers.push(ruleGR.approver.name.toString());
16+
}
17+
18+
return JSON.stringify(approvers);
19+
}
20+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function onSubmit() {
2+
var ga = new GlideAjax('DocumentValidationHelper');
3+
ga.addParam('sysparm_name', 'validateAttachments');
4+
ga.addParam('sysparm_item_id', g_form.getUniqueValue());
5+
ga.getXMLAnswer(function(response) {
6+
if (response !== 'valid') {
7+
alert('Document validation failed: ' + response);
8+
return false;
9+
}
10+
});
11+
return true;
12+
}

0 commit comments

Comments
 (0)