Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

HR Profile GlideRecord

lawrencemgann
Tera Guru

Good afternoon,

 

I'm attempting to use GlideRecord to read and display values from the current logged in user's HR Profile.  I have the following in a before display business rule on the sn_hr_er_case table:

 

 

 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var gr = new GlideRecord('sn_hr_core_profile');
	gr.get(gs.getUserID());
	var sigName = gr.u_legal_name.getDisplayValue();
	var sigTitle = gr.u_business_title.getDisplayValue();

	g_scratchpad.signature = "Thank you," + "\n" + sigName + "\n" + sigTitle;

})(current, previous);

 

 

 

I have a simple UI Action to add the scratchpad values, but the legal name and business title fields aren't populating, only the "Thank you," text.

lawrencemgann_0-1704319660907.png

 

 

Is there perhaps a read/security error with attempting to GlideRecord / copy values from the sn_hr_core_profile table?

 

Thanks for your consideration!

2 ACCEPTED SOLUTIONS

ahefaz1
Mega Sage

@lawrencemgann ,

 

The problem seems to be the use of gs.getUserID() to query the HR profile table.

gs.getUserID will give you the sys_id of the user table record which belongs to the currently logged in user.

 

Please try below

 

var grHRProfile = new GlideRecord('sn_hr_core_profile');
grHRProfile.addEncodedQuery('user.sys_id='+gs.getUserID());
grHRProfile.query(); // Query will execute the glideRecord
grHRProfile.next(); //Will allow the getDisplayValues below to work
var sigName = grHRProfile.u_legal_name.getDisplayValue();
var sigTitle = grHRProfile.u_business_title.getDisplayValue();

g_scratchpad.signature = "Thank you," + "\n" + sigName + "\n" + sigTitle;

 

 

Please mark helpful, if this helped, or accept the solution if it worked.

 

Thanks,

View solution in original post

Sandeep Rajput
Tera Patron
Tera Patron

@lawrencemgann Please update the business rule as follows.

 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var gr = new GlideRecord('sn_hr_core_profile');
    gr.addQuery('user', gs.getUserID()); //You need add filter on the user column on hr profile table
    gr.query();
    if (gr.next()) {
        var sigName = gr.u_legal_name.getDisplayValue();
        var sigTitle = gr.u_business_title.getDisplayValue();
        g_scratchpad.signature = "Thank you," + "\n" + sigName + "\n" + sigTitle;
    }
})(current, previous);

Hope this helps.

View solution in original post

3 REPLIES 3

ahefaz1
Mega Sage

@lawrencemgann ,

 

The problem seems to be the use of gs.getUserID() to query the HR profile table.

gs.getUserID will give you the sys_id of the user table record which belongs to the currently logged in user.

 

Please try below

 

var grHRProfile = new GlideRecord('sn_hr_core_profile');
grHRProfile.addEncodedQuery('user.sys_id='+gs.getUserID());
grHRProfile.query(); // Query will execute the glideRecord
grHRProfile.next(); //Will allow the getDisplayValues below to work
var sigName = grHRProfile.u_legal_name.getDisplayValue();
var sigTitle = grHRProfile.u_business_title.getDisplayValue();

g_scratchpad.signature = "Thank you," + "\n" + sigName + "\n" + sigTitle;

 

 

Please mark helpful, if this helped, or accept the solution if it worked.

 

Thanks,

Sandeep Rajput
Tera Patron
Tera Patron

@lawrencemgann Please update the business rule as follows.

 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var gr = new GlideRecord('sn_hr_core_profile');
    gr.addQuery('user', gs.getUserID()); //You need add filter on the user column on hr profile table
    gr.query();
    if (gr.next()) {
        var sigName = gr.u_legal_name.getDisplayValue();
        var sigTitle = gr.u_business_title.getDisplayValue();
        g_scratchpad.signature = "Thank you," + "\n" + sigName + "\n" + sigTitle;
    }
})(current, previous);

Hope this helps.

lawrencemgann
Tera Guru

Thank you both, these are both very helpful and well explained!