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

Autopopulate field using Scheduled Job

Jeni Sebastian
Mega Contributor

Hello everyone. Im a newbie in servicenow and I'm kinda lost in getting the value of the field in sys_user table .

My requirement is to update every month the "HR Partner EID" of the users in a custom table..  I need to reference it in the sys_user table field named "HR People Advisor Name".

Example: If the HR  people advisor name in the user table is "Maria Foj"  , it should also display the same name in the HR partner EID field that is in the custom table.

 

Custom table field:

 

User Table field:

 

 

What I did was , I created a scheduled job that will run every month but I really didnt get the value from HR people advisor name to be displayed in the HR partner eid field. 

 

 

All your responses will be a great help to me. Thank you

1 ACCEPTED SOLUTION

If it's a reference then it can hold user sysId and not u_hrrepnm

In that case the script I shared will work fine

try{
	var gr = new GlideRecord("custom table");
	gr.addQuery("u_apprentice_statusINactive_loa,active");
	gr.addEncodedQuery("u_apprentice_eidISNOTEMPTY");
	gr.query();
	while (gr.next()) {
		var user = new GlideRecord('sys_user');
		if(user.get('user_name', gr.u_apprentice_eid)){
			gr.u_hr_partner_eid = user.getUniqueValue();
			gr.update();
		}
	}
}
catch(ex){
	gs.info(ex);
}

Regards
Ankur

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

View solution in original post

17 REPLIES 17

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

So you will have to iterate the custom table and check if the apprentice field is empty or not

Can you explain which field in custom table holds the user reference or user_name? based on that you can know

Regards
Ankur

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

Hi @Ankur Bawiskar 

I have to query the custom table to all records that are in "Active" state only because some of the records in the custom table are already in a "complete" state records.. In short, I only just need to update all the HR Partner EID(custom table field)  in "Active" state records every month. 

 

The field that holds the user_name in the custom table is the "u_apprentice_eid".

 

find_real_file.png

 

 

share the script here so that we can help

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

Hi @Ankur Bawiskar . 

Here's what Ive done so far

 

var user= new GlideRecord('sys_user');
var hrpeopleadvisor = user.getValue('u_hrrepnm');//the field where we need to get the value for HR Partner EID field.

 

 

var gr = new GlideRecord('apprentice');//custom table
var hr = gr.u_hr_partner_eid;//custom table field that needs to be updated every month
var id = gr.getValue('u_apprentice_eid');//user in the custom table
gr.addEncodedQuery('u_apprentice_statusINactive_loa,active');//records are active
gr.query();

if(gr.next()){
hr.setValue('hrpeopleadvisor');

}