The CreatorCon Call for Content is officially open! Get started here.

Get User Details from HR Profile using On Change Client Script

Hari7
Kilo Guru

I have to create a HR Form with a variable referencing HR Profile. After selecting the HR Profile, I want to get the employee number and department of the HR Profile from the associated User Profile. 

 

I have tried catalog client scirpt and script include but it is not working for me. It will be great if you could help me out in the code.

 

Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var subjectPerson3 = g_form.getValue('what_is_your_name');
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);
}
}

 

Script Include:

 

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('user');
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 '';
}

 

Hari7_0-1725162707495.png

 

Variables:

 

Hari7_1-1725162848785.png

what_is_your_name - Reference to HR Profile

employee_number - Single Text Field

 

1 ACCEPTED SOLUTION

I was able to split the catalog client script and got it working. 

View solution in original post

4 REPLIES 4

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

"I have tried catalog client scirpt and script include but it is not working for me"

 

What exactly is not working? Please provide us with info, preventing us from spending hours on your question.

 

You could also help yourself by adding debugging. Debugging will help you spot until where the scripting is working, from where its failing, if the values are what you expect, etc.. Instead of hours on something like, you would only be spending minutes.

 

So add debugging. If then still help needed, follow-up on this topic. Do provide us with the info!

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

What exactly is not working? - Employee Number is not getting populated from the HR Profile input.

Okay, good luck in your search!

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

I was able to split the catalog client script and got it working.