- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2022 12:46 AM
Hi all
As part of our New Employee process, we'd like to set 2 values in 2 fields on a Catalog Task record, from the User table.
I've attempted a client script on the Catalog Task table to do this, but I know it's not right, and was hoping someone could put me on the right track with it.
What I've tried to do is create a variable (newname) to get the name entered in a string variable on the catalog form (that variable is called full_name on the form)
I've then queried the User table to find any match of newname in the u_name field which is on the user table.
Then I've said if the employee_number field on the user table for that record is not empty, then set the u_staff_number field on the Catalog Task (which I want to populate) to whatever is in the employee_number field on the user record.
Here it is -
function onLoad() {
//Type appropriate comment here, and begin script below
var newname = ritm.variables.full_name;
var gr = new GlideRecord('sys_user');
gr.addQuery('u_name', newname);
gr.query(myCallbackFunction); //Execute the query with callback function//After the server returns the query recordset, continue here
function myCallbackFunction(gr) {
while (gr.next()) { //While the recordset contains records, iterate through them
if (gr.employee_number != ' ') {
g_form.setValue(u_staff_number = gr.employee_number);
}
}
}
}
Any help would be great!
Thanks
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2022 01:33 AM
Glad to know that my approach worked.
yes like this
Ensure you use if instead of while then
function onLoad() {
//Type appropriate comment here, and begin script below
var newname = g_form.getValue('full_name');
var gr = new GlideRecord('sys_user');
gr.addQuery('u_name', newname);
gr.query(myCallbackFunction); //Execute the query with callback function//After the server returns the query recordset, continue here
function myCallbackFunction(gr) {
if(gr.next()) { //While the recordset contains records, iterate through them
if (gr.employee_number != ' ') {
g_form.setValue('u_staff_number',gr.employee_number);
}
if (gr.email_address != ' ') {
g_form.setValue('u_email',gr.email_address);
}
}
}
}
If my response helped you please mark it correct and close the thread
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2022 12:54 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2022 01:01 AM
Here is the Script
(function executeRule(current, previous /*null when async*/) {
var newname = current.ritm.variables.full_name;
var gr = new GlideRecord('sys_user');
gr.addQuery('u_name', newname);
gr.query();
while (gr.next()) {
if (gr.getValue('employee_number') != ' ') {
current.setValue('u_staff_number', gr.getValue('employee_number'));
}
}
current.update();
})(current, previous);
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2022 01:08 AM
Using current.update() in business rule is not recommended by ServiceNow.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2022 01:10 AM
Before business rule is also not recommended for reqs, ritms, tasks. Current.update() may cause a loop, but here I don't see such danger.
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024