Default value to auto-populate cost center of Requested for user
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2021 07:05 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2021 07:29 AM
Hi,
are you in scoped app?
you can use this onChange client script on Requested for which will work on form load also
1) it would set the cost center of the requested for user
also when user changes it would set the value
function onChange(control, oldValue, newValue, isLoading) {
if(newValue == ''){
g_form.clearValue('cost_center'); // cost center variable name here
}
var ref = g_form.getReference('requested_for', callBackMethod); // requestedFor variable name
}
function callBackMethod(ref){
if(ref.cost_center)
g_form.setValue('cost_center', ref.cost_center); // cost center variable name here
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2021 09:07 PM
Another way
in the default value of variable add this
javascript: var costCenter = ''; var gr = new GlideRecord('sys_user'); gr.get(gs.getUserID()); costCenter = gr.getValue('cost_center'); costCenter;
Then use onChange client script on Requested For
function onChange(control, oldValue, newValue, isLoading) {
if(isLoading)
return;
if(newValue == ''){
g_form.clearValue('cost_center'); // cost center variable name here
}
var ref = g_form.getReference('requested_for', callBackMethod); // requestedFor variable name
}
function callBackMethod(ref){
if(ref.cost_center)
g_form.setValue('cost_center', ref.cost_center); // cost center variable name here
}
Let me know if I have answered your question.
If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2022 12:12 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2022 07:47 AM
Please post a new question as this is quite older one and tag me there.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2021 07:32 AM
Have you tried what I mentioned? Simulated myself, works fine.
If it concerns onChange: with Quebec don't go for a scripted solution anymore. See the article I mentioned. ServiceNow introduced a really nice no-code solution for this.
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP
---
LinkedIn
Community article, blog, video list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field