Glide record HR profile table to fetch Employee number and Domain ID

Renu9
Tera Contributor

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. 

21 REPLIES 21

Vishal Birajdar
Giga Sage

Hi Renu,

You can use client callable script include and clien script on catalog item as below:

Here I have used ‘sys_user’ table  to get information of logged in user.

Step 1 : Script Include

Use below function in script include.Make sure its client callable

 

demoFunction : function(){             

                //gs.addInfoMessage('inside SI');

                var userObject = {};                    // create empty object to store User info

                var loggedUser = this.getParameter('sysparm_user');   //get the UserId of current login user

                var gr = new GlideRecord('sys_user');   // glide record on HR profile table, here i have used 'sys_user' table

                gr.addQuery('sys_id',loggedUser);     //query with 'loggedUser'

                gr.query();

                if(gr.next()){

                                userObject.first_name = gr.first_name.toString();// here you can use your custom fields i.e reqgr.u_domain_id.toString()

                                userObject.last_name = gr.last_name.toString();//here you can use your custom fields i.e.reqgr.employee_id.toString()

                                               

                                }

                                val = JSON.stringify(userObject);   // need to stringfy the userObject

                                //gs.addInfoMessage(val);

                                return val;    // return userObject values to client side

                },

 

Step 2 :Catalog Client Script

Type : onLoad

   var loggedUser = g_user.userID;   //get the userID of logged in user

   var ga = new GlideAjax('getHRProfileCommunity');  //name of your script include

   ga.addParam('sysparm_name','demoFunction');// name of function whithin script include

   ga.addParam('sysparm_user',loggedUser);  // value which needs to send to script include

   ga.getXML(callBackFun); // make a call back function

               

   function callBackFun(response){

                               

                var answer = response.responseXML.documentElement.getAttribute('answer');

                var val = JSON.parse(answer);

// here i have set the first_name and last name fields on form

                g_form.setValue('first_name',val.first_name); //here u can set values of fields on form i.e 'us_domain_id'

                g_form.setValue('last_name',val.last_name);   //here u can set values of fields on form i.e  'employee_number'

               

}

 

Hope this is what you are looking for......!!!!

Please mark correct/Helpful if its helpful to you......!!!!

 

 

 

Thanks & Regards,

Vishal Birajdar

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Hi Vishal, 

Thanks for the response. I have now modified my Catalog client script and script include in the way you mentioned.

Script Include:

var Requester_Details = Class.create();
Requester_Details.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
demoFunction:function(){
gs.addErrorMessage("SI Start");
var userObject ={};
var loggedUser= this.getParameter('sysparm_user');
//gs.addErrorMessage(loggedUser);
var hrp = new GlideRecord('sn_hr_core_profile');
gr.addQuery('sys_id',loggedUser);
gs.addErrorMessage(loggedUser);
gr.Query();
if(gr.next())
{
userObject.us_domain_id=gr.u_domain_id.toString();
userObject.employe_id=gr.employee_number.toString();
}
val = JSON.stringify(userObject);
return val;
},
type: 'Requester_Details'
});

 

Catalog client script : 

function onLoad() {
var loggedUser = g_user.userID;
var ga = new GlideAjax('Requester_Details');
ga.addParam('sysparm_name','demoFunction');
//alert("into SI");
ga.addParam('sysparm_user',loggedUser);
//alert(loggedUser);
ga.getXML(callBackFun);

function callBackFun(response)
{
var answer= response.responseXML.documentElement.getAttribute("answer");
var val= JSON.parse(answer);
g_form.setValue('us_domain_id',val.u_domain_id);
g_form.setValue('employee_number', val.employee_id);

}
}

But im getting 'There is a JavaScript error in your browser console' error message . 

In the script include, i tried giving gs.addErrorMessage(loggedUser); to view the loggedUser. No popup is getting displayed. Do I need to change anything here. Please suggest.

Hi Renu,

Here you have used 'hrp' as variable name and and below you have used 'gr' in script include,

please change it to 'hrp'.

also in gr.Query() ,  'Q' should be in small case e.g., hrp.query();

var hrp = new GlideRecord('sn_hr_core_profile');
gr.addQuery('sys_id',loggedUser);
gs.addErrorMessage(loggedUser);
gr.Query();
if(gr.next())
{
userObject.us_domain_id=gr.u_domain_id.toString();
userObject.employe_id=gr.employee_number.toString();
}

 

 

Warm regards,

Vishal Birajdar

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Thanks for letting know. I have modified accordingly and I see loggeduser sys I'd in HR profile table. But gr.query() is resulting 0 rows. Not sure what's missing here.

If possible can you please share screenshot of 'HR profile table' LIST VIEW

So that we can query on that table accordingly.

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates