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 Its reference to user table.

Hi @preethigovi ,

 

your script your fetching display value then setting that with the reference field so its not work.

 

var hrProfileGR = new GlideRecord('sn_hr_core_profile');
hrProfileGR.addQuery('number=HRP3083319^u_managerISEMPTY'); 
gs.info('PT1');
hrProfileGR.query();
if (hrProfileGR.next()) {
gs.info('PT Success');
var userGR = new GlideRecord('sys_user'); 
if (userGR.get(hrProfileGR.user)) {
var hiringManager = userGR.manager; 
hrProfileGR.setValue('u_manager', hiringManager); 
hrProfileGR.update(); // Save changes
gs.info('Hiring Manager set to: ' + hiringManager);
} else {
gs.info('User record not found.');
}
}

preethigovi
Tera Contributor

@Ankur Bawiskar @Bhavya11 

preethigovi_0-1734321574412.png

 

In show xml it shows value but in field its empty.

@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

@preethigovi 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

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