- 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 01:42 AM
That's perfect - thank you!