Dynamic Population of Lead Architect Select Box
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
- The Lead Architect field should be populated dynamically with relevant users from the IT Architecture Chapter. Only active users with the title containing “Lead Architect” will appear in the list. The list updates automatically based on form changes and does not require a reference field.
script include :
var LeadArchitectList = Class.create();
LeadArchitectList.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAllUsers: function() {
var gr = new GlideRecord('sys_user');
gr.addEncodedQuery(
"department.name=IT Architecture Chapter" +
"^titleLIKELead Architect" +
"^active=true" +
"^ldap_server=8cc39bf5db2a830036c398f3db9619dd" +
"^u_status=aktywny"
);
gr.query();
var users = [];
while (gr.next()) {
users.push({
value: gr.getValue('sys_id'), // wartość do select boxa
label: gr.getDisplayValue('name') // etykieta w select boxie
});
client scripts:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) return;
g_form.clearOptions('lead_architects'); // czyścimy select box
var ga = new GlideAjax('LeadArchitectList');
ga.addParam('sysparm_name', 'getAllUsers');
ga.getXMLAnswer(function(answer) {
console.log('Answer from Script Include:', answer);
if (!answer) return;
var resp = JSON.parse(answer);
if (resp.users && resp.users.length > 0) {
resp.users.forEach(function(u) {
g_form.addOption('lead_architects', u.value, u.label);
});
}
});
}
}
return JSON.stringify({users: users});
},
type: 'LeadArchitectList'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Czesc @Pawel Barcik.,
what debugging did you do? Any error message?
On first sight - the script include doesn't return anything and the client scrip has addOption() while it's most likely a reference, I don't think that will work.
Have you tried to log it? What's the behaviour - nothing happens, it allows all users not just the leads, or no users, ... please provide some context
/* If my response wasn’t a total disaster ↙️ ⭐ drop a Kudos or Accept as Solution ✅ ↘️ Cheers! */
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
The script include has multiple problems. The following may work better:
var LeadArchitechList = Class.create();
LeadArchitechList.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAllUsers: function() {
var gr = new GlideRecord('sys_user');
gr.addEncodedQuery(
"department.name=IT Architecture Chapter" +
"^titleLIKELead Architect" +
"^active=true"
"^ldap_server=8cc39bf5db2a830036c398f3db9619dd" +
"^u_status=aktywny"
);
gr.query();
var users = [];
while (gr.next()) {
users.push({
value: gr.getValue('sys_id'), // wartość do select boxa
label: gr.getDisplayValue('name') // etykieta w select boxie
});
}
return users;
},
type: 'LeadArchitechList'
});
Client script appears to be inserted withing the script include code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
try below corrected scripts:
client script-
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) return;
g_form.clearOptions('lead_architects');
var ga = new GlideAjax('LeadArchitectList');
ga.addParam('sysparm_name', 'getAllUsers');
ga.getXMLAnswer(function(answer) {
if (!answer) return;
var resp = JSON.parse(answer);
if (resp.users && resp.users.length > 0) {
resp.users.forEach(function(u) {
g_form.addOption('lead_architects', u.value, u.label);
});
}
});
}
script include-
var LeadArchitectList = Class.create();
LeadArchitectList.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAllUsers: function() {
var gr = new GlideRecord('sys_user');
gr.addEncodedQuery(
"department.name=IT Architecture Chapter" +
"^titleLIKELead Architect" +
"^active=true" +
"^ldap_server=8cc39bf5db2a830036c398f3db9619dd" +
"^u_status=aktywny"
);
gr.query();
var users = [];
while (gr.next()) {
users.push({
value: gr.getValue('sys_id'),
label: gr.getDisplayValue('name')
});
}
return JSON.stringify({users: users});
},
type: 'LeadArchitectList'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi @Pawel Barcik ,
I have modified your code, please check these...
Script Include...(Ensure Script Include is Client Callable)
var LeadArchitectFetcher = Class.create();
LeadArchitectFetcher.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
fetchArchitects: function() {
// Parameters if you want to pass filters from client
var deptName = this.getParameter('sysparm_department') || 'IT Architecture Chapter';
var titleFilter = this.getParameter('sysparm_title_contains') || 'Lead Architect';
var userList = [];
var userGR = new GlideRecord('sys_user');
userGR.addQuery('active', true);
userGR.addQuery('title', 'CONTAINS', titleFilter);
userGR.addQuery('department.name', deptName);
userGR.query();
while (userGR.next()) {
userList.push({
sys_id: userGR.getValue('sys_id'),
name: userGR.getDisplayValue('name'),
});
}
return JSON.stringify({ architects: userList });
},
type: 'LeadArchitectFetcher'
});
OnLoad Client Script..
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) return;
var fieldName = 'lead_architect'; // replace with your select field’s name
g_form.clearOptions(fieldName);
var ga = new GlideAjax('LeadArchitectFetcher');
ga.addParam('sysparm_name', 'fetchArchitects');
// optional: you can pass department or title substring if needed
// ga.addParam('sysparm_department', 'IT Architecture Chapter');
// ga.addParam('sysparm_title_contains', 'Lead Architect');
ga.getXMLAnswer(function(response) {
if (!response) return;
var parsed = JSON.parse(response);
if (parsed.architects && parsed.architects.length > 0) {
parsed.architects.forEach(function(a) {
g_form.addOption(fieldName, a.sys_id, a.name);
});
} else {
// Optional: add a “No Architect Found” option
g_form.addOption(fieldName, '', '-- No Lead Architect Available --');
}
});
}
If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/