Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Hi,

can you share these details?

hrpeopleadvisor -> field on user table which needs to be fetched

u_apprentice_eid -> on table apprentice is holding reference to User table or holds user_name

u_hr_partner_eid -> field on apprentice table which will be populated

As per my assumptions here is the script; enhance it

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

@Jeni Sebastian 

Did you get a chance to check on the script I shared above?

Regards
Ankur

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

Hi @Ankur Bawiskar . I tried the script above but nothing happened, it didnt changed the HR PArtner EID field value in the custom table .

Fields:

HR People Advisor -> u_hrrepnm

-field on user table which needs to be fetched

Apprentice EID  -> u_apprentice_eid 

-on table apprentice is holding reference to User table or holds user_name

HR Partner EID -> u_hr_partner_eid 

-field on apprentice table which will be populated

 

 I also tried to make changes and query a specific record in the custom table to see if its working in one record ,but still not working...

 

 

var gr = new GlideRecord('customtable');
gr.addEncodedQuery('u_apprentice_statusINactive_loa,active');
gr.addEncodedQuery('u_apprentice_eid=044fcb89db19b09031df7a43e29619f9Y');
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.getDisplayValue('u_hrrepnm');
gr.update();
}
}

Hi,

u_hr_partner_eid on custom table is reference right then how can it hold string field from user record?

Regards
Ankur

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

Hi @Ankur Bawiskar 

Yes the u_hr_partner_eid field reference is the sys_user.

 

 

find_real_file.png