Script returns undefined for reference value

preethigovi
Tera Contributor

Hi Team,

I was learning on scripting where i had uses case to fetch manager (reference to user table) in HR profile and populate on filed on other scoped table

 

var hrProfileGR = new GlideRecord('sn_hr_core_profile');
hrProfileGR.addQuery('sys_id=04556f50471f1110e72d7116536d43f9');
hrProfileGR.query();
var managerValue = hrProfileGR.getValue('manager');
var userValue = hrProfileGR.getDisplayValue('user')
gs.info('PreeManager value: ' + managerValue);
gs.info('PreeManager2 value: ' + userValue);
 
Can you point out why its returning null value for managers?
 
preethigovi_0-1733471691042.png

 

1 ACCEPTED SOLUTION

@preethigovi 

use this script

var hrProfileGR = new GlideRecord('sn_hr_core_profile');
hrProfileGR.query();
while (hrProfileGR.next()) {
    var userValue = hrProfileGR.getValue("user");
    var userProfile = new GlideRecord('sys_user');
    if (userProfile.get(userValue)) {
        hrProfileGR.u_manager = userProfile.manager;
        hrProfileGR.update();
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

14 REPLIES 14

Bhavya11
Kilo Patron

Hi @preethigovi ,

 

Hope all the field backend value is correct .please add hrProfileGR.next() it will work

var hrProfileGR = new GlideRecord('sn_hr_core_profile');
hrProfileGR.addQuery('sys_id=04556f50471f1110e72d7116536d43f9');
hrProfileGR.query();
if(hrProfileGR.next()){
var managerValue = hrProfileGR.getValue('manager');
var userValue = hrProfileGR.user.getDisplayValue();
gs.info('PreeManager value: ' + managerValue);// return sys_id if it is refernece field
gs.info('PreeManager2 value: ' + userValue); 
}

 

 

Please mark this as Correct or Helpful if it helps.

Thanks,

BK

Yea @Bhavya11  Thank You its my mistake of not noticing. 

Hi @preethigovi ,

 

if it solve issue please mark it as an "accepted solution".

 

Thanks,

BK

Abhishek_Thakur
Mega Sage

Hello @preethigovi ,

You can follow the below script;

var hrProfileGR = new GlideRecord('sn_hr_core_profile');
hrProfileGR.addQuery("sys_id","b0e01de0832c5a1461ae9f65eeaad361"); //sys id of HR profile record
hrProfileGR.query();
if(hrProfileGR.next()){
var userValue = hrProfileGR.getValue("user");
var userProfile = new GlideRecord('sys_user');
userProfile.addQuery("sys_id",userValue);
userProfile.query();
if(userProfile.next()){
       var userValue = hrProfileGR.getDisplayValue("user");
	var managerValue = userProfile.manager.name;
gs.info("PreeManager Value is: "+ userProfile.manager.name);
gs.info('PreeManager value: ' + managerValue);
gs.info('PreeManager2 value: ' + userValue);
}

 

Please mark my solution as accepted and give thumbs up, if it helps you.

 

Regards,

Abhishek Thakur

it will return the manager and the username.