Glide record HR profile table to fetch Employee number and Domain ID
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2021 08:57 AM
I need to auto populate Employee number and domain id of the logged in user once the Service is opened from the portal.
I am writing an onLoad function in catalog client script and glide recording the sn_hr_core_profile table. number is the unique field in HR Profile table. But I am receiving an undefined message at the alert i used alert(g_user.number);. Can you please suggest me what i m missing here.
function onLoad() {
var reqgr = new GlideRecord('sn_hr_core_profile');
reqgr.addQuery('sys_id', g_user.number);
alert(g_user.number);
reqgr.query();
while(reqgr.next())
{
g_form.setValue('us_domain_id', reqgr.u_domain_id);
g_form.setValue('employee_number', reqgr.employee_id);
}
}
Let me know if anything needed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2021 09:09 AM
Hello,
You shouldn't use GlideRecord in any client side scripting. Also, on the forums, just to mention this, please use the Insert/Edit code sample button to make things easier to follow:
If you were using out of box glidesystem support fields, such as department, email, location, etc. you could set the default value for these service portal fields to the equivalent:
https://servicenowguru.com/scripting/user-object-cheat-sheet/
However, since you're using other fields such as a custom field named: "u_domain_id" and then the employee ID field, it's best practice to communicate with the server to retrieve this information and then back this back to the client.
So since this all appears to be stuff you're wanting set as Default, you can use the default field within the field settings to help with this, for example:
https://docs.servicenow.com/bundle/paris-platform-administration/page/administer/field-administratio...
Or...as part of the onLoad, call a script include via GlideAjax to retrieve the information and then set the values properly. Here's a cheat sheet for glideajax to assist you: https://community.servicenow.com/community?id=community_article&sys_id=9f7ce2e1dbd0dbc01dcaf3231f961...
For g_user.number not working...that's because it's unsupported, not sure if you saw that somewhere? But that's not valid.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2021 05:31 AM
Hello,
I'm glad you see you've adjusted your client script to not use GlideRecord in it and instead are using GlideAjax. I'm also glad to see you aren't using "g_user.number" since that wasn't supported anyway.
All of which I mentioned above.
Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2021 05:36 AM
Hello,
I'm glad you see you've adjusted your client script to not use GlideRecord in it and instead are using GlideAjax. I'm also glad to see you aren't using "g_user.number" since that wasn't supported anyway.
All of which I mentioned above.
If my reply helped guide you Correctly, please mark as Correct.
Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2021 12:24 AM
Hi Allan,
I'm having a similar issue but on a business rule running on the HR Case table [sn_hr_core_case]. I can't seem to get SOME of the data stored on the HR Profile record - the user sys_id is what I'm trying to retrieve with no help.
First time configuring a scoped app and realizing how different it is from Global scope development. Any tips on resources I can find to guide me in Scoped App development?
Now to my main question: how do I get the user sys_id stored on the HR Profile record that is linked to the HR Case? (used the insert/edit code 😉 )
employeeNumber2 returns a value but user2 does not.
// testing script for CSV compilation
//gs.info('AN: BR run on HR Case Table');
var hrProfile = current.opened_for; //Ok
var absenceCode = '11';
var employeeNumberDotted = current.opened_for.getRefRecord().employee_number;//OK
//var user = current.opened_for.getRefRecord().user;//not working. returns 'undefined'
var user2 = current.opened_for.getRefRecord().getValue('user');//returns 'null'
var employeeNumber2 = current.opened_for.getRefRecord().getValue('employee_number');//returns a value
gs.info('AN: hrProfile : ' + hrProfile);//OK
gs.info('AN: user dotted ' + user + ' userGet value : ' + user2 + ' employeeNumber2 getValue : ' + employeeNumber2);
