populate cost center code in default logged in user

Anubhav Srivas1
Tera Expert

Hello,

For logged in user i need to show the cost center code as default,

 

I have tried the below code in default value but it is not working.

javascript: var gr= new GlideRecord('sys_user');gr.get(gs.getUserID());gr.getValue('cost_center');

2 REPLIES 2

Sonu Parab
Mega Sage
Mega Sage

Hello @Anubhav Srivas1 ,

 

To show the cost center code as the default value for a field in ServiceNow, you can use the following code in the default value script:

 

javascript: (function() {
var gr = new GlideRecord('sys_user');
gr.get(gs.getUserID());
return gr.getValue('cost_center');
})();

 

Make sure to set the default value script of the field to the above code. This script retrieves the current logged-in user's sys_user record and gets the value of the cost_center field. The returned value will be used as the default value for the field.

 

Please mark this response as correct and/or helpful if it assisted you

Thank you.

 

Hi,

i'm getting the cost center sys id when i try this. I need the cost center code.

 

cmn_cost_center.do?sys_id=ea57131fdbc6c30002ea788bbf961911&sysparm_view=

 

the sys id shows above is displaying and not the actual value of the cost center.