The CreatorCon Call for Content is officially open! Get started here.

Setting a value in a Catalog Task field from the User table

sarahleighton
Tera Contributor

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

1 ACCEPTED SOLUTION

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

10 REPLIES 10

Martin Ivanov
Giga Sage
Giga Sage

Try with After insert business rule like this one.

Let me know if it works fine.

Mark Correct AND Helpful. Thanks!

find_real_file.png


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

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

Using current.update() in business rule is not recommended by ServiceNow.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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