- 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:25 AM
Before BR can be used to set field values and won't require current.update()
In after business rule also current.update() should not be used as per practice
Recommended Practices in Using current.update() in Business Rules
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:08 AM
Hi,
you can add that full_name variable on catalog task variable editor by adding it during the Catalog Task workflow activity
Add the variable from Available to Selected slushbucket during catalog task creation
If you wish you can hide it on catalog task form; the reason we added that variable is to have easy access using g_form object to get that variable value
then you can use the script
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) {
while (gr.next()) { //While the recordset contains records, iterate through them
if (gr.employee_number != ' ') {
g_form.setValue('u_staff_number',gr.employee_number);
break;
}
}
}
}
If you don't wish to add then use display business rule on sc_task table to get that variable value and store in g_scratchpad and then use in your client script
Display BR: sc_task
g_scratchpad.myValue = current.variables.full_name;
Client Script:
function onLoad() {
//Type appropriate comment here, and begin script below
var newname = g_scratchpad.myValue;
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);
break;
}
}
}
}
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:26 AM
Thank you for marking my response as helpful.
If it helped please mark it correct and close the thread so that it benefits future readers.
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:27 AM
That works perfectly! Thanks Ankur!
If I wanted to add populate another field on the Catalog Task (the user's email address, with field name email_address on the user record), from the same user record, how would I add in the if statement to see if the email address is not empty and then populate it? I understand that I'd need -
if (gr.email_address != ' ') {
g_form.setValue('u_email',gr.email_address);
But I don't quite know how to structure that into the script with the other if statement, because I want both to run?
Thanks very much
Sarah
- 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