field not populated in ESC view
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 05:31 AM
function onChange(control, oldValue, newValue, isLoading) {
// Prevent the script from running if the form is loading or the new value is empty
if (isLoading || newValue === '') {
return;
}
// Get the sys_id from the 'sysid' field
var memberSysID = newValue;
// Create a GlideRecord object for the 'sys_user_grmember' table
var grMember = new GlideRecord('sys_user_grmember');
if (grMember.get(memberSysID)) {
// If the record is found, retrieve the 'group' sys_id from the member record
var groupSysID = grMember.getValue('group');
// Create another GlideRecord object for the 'sys_user_group' table
var grGroup = new GlideRecord('sys_user_group');
if (grGroup.get(groupSysID)) {
// Retrieve the 'name' property from the group record
var groupName = grGroup.getValue('name');
// Set the value of the 'group_sysid' field
g_form.setValue('group_sysid', groupName);
} else {
// Clear the 'group_sysid' field if no group record is found
g_form.setValue('group_sysid', '');
}
} else {
// Clear the 'group_sysid' field if no member record is found
g_form.setValue('group_sysid', '');
}
}
The following script is supposed to retrieve the sysID field value and then query groups table to retrieve the name of the table:
it works on the backend, but when i test it in ESC this field is not getting populated:
any ideas why?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 05:44 AM
@dev_K This is expected as GlideRecord queries are not allowed in Service Portal. Since this is supported in the platform view hence it is working there but it will not work on Service portal. In order to make it work on the Service Portal you need to use a combination of GlideAjax + a Server side script include.
Here is an example.
Client Script:
// Client Script - onChange
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return; // Don't run the script if the form is loading or the new value is empty
}
// Create a new GlideAjax object, calling the server-side script include 'GetGroupByMember'
var ga = new GlideAjax('GetGroupByMember');
// Add the member sys_id (newValue) as a parameter
ga.addParam('sysparm_sys_id', newValue);
// Make the asynchronous call to the Script Include
ga.getXMLAnswer(function(response) {
var groupSysId = response.responseXML.documentElement.getAttribute("answer");
if (groupSysId) {
console.log('Group Sys ID: ' + groupSysId);
g_form.setValue('group_sysid',groupSysId );
// Perform any additional actions like populating a field with the group sys_id
} else {
console.log('No group found for this member.');
}
});
}
Script Include: Client callable script include code
// Script Include - GetGroupByMember
var GetGroupByMember = Class.create();
GetGroupByMember.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// This method will be called by the client-side GlideAjax call
getGroupByMember: function() {
// Get the member sys_id from the client script
var memberSysId = this.getParameter('sysparm_sys_id');
// Query the sys_user_grmember table (Group Member table)
var groupMemberGR = new GlideRecord('sys_user_grmember');
groupMemberGR.addQuery('user', memberSysId); // Assuming 'user' is the reference to the member
groupMemberGR.query();
if (groupMemberGR.next()) {
// Return the sys_id of the group the member belongs to
return groupMemberGR.group.toString(); // Assuming 'group' is the reference field to the group
}
// If no group found for the member, return an empty string
return '';
}
});
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 06:06 AM
when i create a script include im asked the following after i hit submit:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 06:12 AM
@dev_K This is a required step, here you need to select the role which your users will have when they will use this form.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 07:22 AM
This is not working.
here is the error in the console: