The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to get value from reference field

preethigovi
Tera Contributor

Hi Team,

Iam learning scripting and i have one use case.

Get the manager reference to user table from HR profile and populate on other scoped table field.

 

var hrProfileGR = new GlideRecord('sn_hr_core_profile');
hrProfileGR.addQuery('sys_id=04556f50471f1110e72d7116536d43f9'); //sys id of HR profile record
hrProfileGR.query();
var managerValue = hrProfileGR.getValue('manager');
var userValue = hrProfileGR.getDisplayValue('user')
gs.info('PreeManager value: ' + managerValue);
gs.info('PreeManager2 value: ' + userValue);
 
Here i get manager value as undefined can you help me to trouble shoot
4 REPLIES 4

anshul_goyal
Kilo Sage

Hello @preethigovi,

In your code if statement is missing, try below code:

var hrProfileGR = new GlideRecord('sn_hr_core_profile');
hrProfileGR.addQuery('sys_id=04556f50471f1110e72d7116536d43f9'); //sys id of HR profile record
hrProfileGR.query();
if (hrProfileGR.next()) {
  var managerValue = hrProfileGR.getValue('manager');
  var userValue = hrProfileGR.getDisplayValue('user')
  gs.info('PreeManager value: ' + managerValue);
  gs.info('PreeManager2 value: ' + userValue);
}

 

Please mark my solution as Accepted and Helpful, if it works for you in any way!

Thanks

Shruti D
Kilo Sage

Hello @preethigovi 
Please add the if statement in the code. Please refer the below updated code:

var hrProfileGR = new GlideRecord('sn_hr_core_profile');
hrProfileGR.addQuery('sys_id', '04556f50471f1110e72d7116536d43f9'); // Use the sys_id you are querying for
hrProfileGR.query();

if (hrProfileGR.next()) {
    var managerValue = hrProfileGR.manager;  
    var userValue = hrProfileGR.user.getDisplayValue();  // Display value of the user reference
    gs.info('Manager value: ' + managerValue);
    gs.info('User value: ' + userValue);
}

 

Please Mark Correct ✔️ if this solves your query and also mark Helpful  👍 if you find my response worthy based on the impact.

Regards,
Shruti

preethigovi
Tera Contributor

Hi @Shruti D @anshul_goyal  Oops My bad it was my blunt mistake Thank You

Abhishek_Thakur
Mega Sage

Hello @preethigovi ,

As manager field is available in the related list section. So, you need to glide to user table based on the HR profile record. Here is the below script you can follow as it is working in my PDI.

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 managerValue = userProfile.manager.name;
        var userValue = hrProfileGR.getDisplayValue("user");
gs.info("PreeManager2value is: "+ managerValue)
gs.info('PreeManager2 value: ' + userValue);
}

 

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

 

Regards,

Abhishek Thakur