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

@dev_K Could you please share the code of your client script and script include. Need to check if it is syntactically correct.

I just copy pasted the client script and script include you provided. Basically want I want is to take the sysId available in the 'sysid' field and query table sys_user_grmember for this record to retrieve 'group' property'. This should return a sysID that I want to populate 'group_sysid' field with. It worked with using GlideRecord on the backend only...

 

@dev_K Please see if the following change works for you.

 

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(getGroupByMember);

function getGroupByMember(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.');
        }
    }
}

I copied exactly the same code and i got these console error messages:

 

dev_K_0-1727707495017.png

 

dev_K
Tera Contributor

dev_K_1-1727707556495.png