How to get user information from HR Profile

rafas_2703
Tera Guru

Hi guys, hope you are doing well!

 

I have a record producer with some variables (employee_number, employee_grade, etc), and I want to auto populate those variables with the user on the field with the value "subject_person3". I tried a Script Include but It is not working. Can someone help?

 

Cheers,

Sérgio 

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

Hi Sérgio,

You can use getReference or a Glide Ajax call onChange of the subject_person3 variable

https://docs.servicenow.com/bundle/tokyo-api-reference/page/app-store/dev_portal/API_reference/Glide... 

 

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

 

If you get stuck with either of these, post your script(s) and we can troubleshoot.

Hi Brad,

I have the following Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var subjectPerson3 = g_form.getValue('subject_person3');

if (subjectPerson3) {
var ga = new GlideAjax('AskHR_UtilitiesAJAX');
ga.addParam('sysparm_name', 'getUserProfile');
ga.addParam('sysparm_hrprofile', subjectPerson3);
ga.getXMLAnswer(getUserProfileEmployee_Number);
}
}

function getUserProfileEmployee_Number(answer) {
if (answer) {
var returneddata = JSON.parse(answer);
g_form.setValue('employee_number', returneddata.employee_number);
}

}

 

And the following Script Include function:

getUserProfile: function() {
var hrProfile = this.getParameter('sysparm_hrprofile');
var hrProfileGr = new GlideRecord('sn_hr_core_profile');
hrProfileGr.addQuery('sys_id', hrProfile);
hrProfileGr.query();

if (hrProfileGr.next()) {
var userSysId = hrProfileGr.getValue('name');
var userGr = new GlideRecord('sys_user');
userGr.addQuery('sys_id', userSysId);
userGr.query();

if (userGr.next()) {
var employeeNumber = userGr.getValue('employee_number');
var results = {
'employee_number': employeeNumber
};
return new JSON().encode(results);
}
}

return '';
},

 

If you could help that would be amazing.

Brad Bowman
Kilo Patron
Kilo Patron

Let's start with the basics and add alert lines to the client script before and after/inside the if(subjectPerson3) to make sure it's running and evaluating correctly.  Make sure your Script Include is Client callable, has exactly the same name as is used in the Client Script, and is in the same scope as the record producer/client script.  Then add some gs.addInfoMessage or gs.info lines to the Script Include to confim it's running, and see how far it is getting, the records returned by GRs, etc.

 

It looks like inside your hrProfileGr you want the value of the field named 'user' not 'name' to get to the sys_user table, so

var userSysId = hrProfileGr.getValue('user');

should be able to find the sys_user record to get the employee_number and whatever else.

I made that alteration but forgot to change it in the past, Sorry, but it is still not working, It is not populating the field yet.