field not populated in ESC view

dev_K
Tera Contributor

 

 

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:

 

dev_K_1-1727699425578.png

 

 

it works on the backend, but when i test it in ESC this field is not getting populated:

 

dev_K_2-1727699487538.png

 

 

 

any ideas why?

10 REPLIES 10

Sandeep Rajput
Tera Patron
Tera Patron

@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.

when i create a script include im asked the following after i hit submit: 

Select a user role for Access Control on this Client Callable Script Include
Lookup using list
Cancel  

@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. 

This is not working. 

 

here is the error in the console:  

 

dev_K_0-1727706138499.png