- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2025 03:09 AM - edited ‎02-03-2025 03:17 AM
Hey guys!
I am working with a Record Producer. I am including a variable set called 'rail_variant_ekonomiservice'. In this variable set there is a reference variable called 'refers_to_user' (reference to sys_user table).
Who you choose in the refers_to_user variable shall affect a custom reference variable available directly on the Record Producer, called 'company'.
So.. chosen value in refers_to_user should autopopulate the 'company' variable automatically with his/hers company. The custom variable called company is a reference to core_company table.
OBS, directly on the sys_user table, there is a reference field called 'company' (just like my custom variable based on core_company).
I have the follwing ScriptIncludes that could be used:
var GetUserCompany = Class.create();
GetUserCompany.prototype = Object.extendsObject(AbstractAjaxProcessor, {
//pass in user sys_id to this script include and get users company returned
GetUserCompany: function(){
var sysID = this.getParameter('sysparm_sys_id');
var user = new GlideRecord('sys_user');
user.get(sysID);
var company = user.getValue('company');
return company;
},
type: 'GetUserCompany'
});
I have this record producer client script, but it is not working:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
gForm.clearValue('company'); // Rensa fältet om inget val görs
return;
}
// Anropa Script Include via GlideAjax
var ga = new GlideAjax('GetUserCompany');
ga.addParam('sysparm_name', 'GetUserCompany'); // Metodnamn i Script Include
ga.addParam('sysparm_sys_id', newValue); // Skickar användarens sys_id
ga.getXMLAnswer(function(response) {
if (response) {
gForm.setValue('company', response); // Sätt företagets sys_id i company-fältet
} else {
gForm.clearValue('company'); // Töm fältet om inget företag hittas
}
});
}
Any solution? The record producer is in another scope, while the ScriptIncludes is in the Global one, but is accessible from all scopes.
Thanks in advance!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2025 03:18 AM
your syntax for g_form is wrong
try this
the correct syntax is g_form and not gForm
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
g_form.clearValue('company'); // Rensa fältet om inget val görs
return;
}
// Anropa Script Include via GlideAjax
var ga = new GlideAjax('GetUserCompany');
ga.addParam('sysparm_name', 'GetUserCompany'); // Metodnamn i Script Include
ga.addParam('sysparm_sys_id', newValue); // Skickar användarens sys_id
ga.getXMLAnswer(function(response) {
if (response) {
g_form.setValue('company', response); // Sätt företagets sys_id i company-fältet
} else {
g_form.clearValue('company'); // Töm fältet om inget företag hittas
}
});
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2025 03:18 AM
your syntax for g_form is wrong
try this
the correct syntax is g_form and not gForm
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
g_form.clearValue('company'); // Rensa fältet om inget val görs
return;
}
// Anropa Script Include via GlideAjax
var ga = new GlideAjax('GetUserCompany');
ga.addParam('sysparm_name', 'GetUserCompany'); // Metodnamn i Script Include
ga.addParam('sysparm_sys_id', newValue); // Skickar användarens sys_id
ga.getXMLAnswer(function(response) {
if (response) {
g_form.setValue('company', response); // Sätt företagets sys_id i company-fältet
} else {
g_form.clearValue('company'); // Töm fältet om inget företag hittas
}
});
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2025 04:40 AM
OMG, thanks! I was too blind to see this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2025 01:07 PM
Two things I'd like to bring up.
1. You could configure the "Auto-populate" tab for the Company variable like so:
I forget when it was added, but it's been a few versions.
2. if you go the scripted route, you really should get both the sys_id and display value for the variable. Otherwise you are making an server call to get the sys_id via Ajax, setting the value of the Company variable and by doing that, another server call is being made behind the scenes to get the display value. If you read this post, TNT: Returning Data from GlideAjax Calls, it explains how to get all the info you need in 1 call to the server.