Client Script to Auto-Populate Field on Incident

appstorm
Tera Contributor

Stuck with an onChange client script that uses synchronous GlideAjax to auto-populate the field 'desk location' on behalf of.  

Client Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   var ga = new GlideAjax('x_g_irm_dos_inc.DeskLocationUtils');
    ga.addParam('sysparm_name', 'getLocation');
    ga.addParam('sysparm_desk', g_form.getValue('sys_user'));
    ga.getXML(autoPopulate);

    function autoPopulate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer != '' && answer != g_form.getValue('desk location')) {
            g_form.setValue('desk location', answer);
        }

    }
   
}

 

Script Include

DeskLocationUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getDesk: function() {
        var dsk = this.getParameter('sysparm_desk');
        var gr = new GlideRecord('sys_user');
        gr.addQuery('u_desk_location', dsk);
        gr.query();
        if (gr.next()) {
            var desk = gr.sys_id;
        }
        return desk;
    },

    getLocation: function() {
        var loc;
        var dsk = this.getParameter('sysparm_desk');
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', dsk);
        gr.query();
        if (gr.next()) {
            u_desk_location = gr.u_desk_location;
        }
        return loc;
    },

    type: 'DeskLocationUtils'
});

Desk location is a string field on the Incident table named x_g_irm_dos_inc_desk_location, while the desk location field name on the user table is u_desk_location.  Any help with the above script(s) is greatly appreciated!

Thank you!  

7 REPLIES 7

Mohith Devatte
Tera Sage
Tera Sage

Hello,

please replace your scripts like below and if you want logged in user Sys_id i replaced with g_user.userID which i replaced below in your script

client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   var ga = new GlideAjax('x_g_irm_dos_inc.DeskLocationUtils');
    ga.addParam('sysparm_name', 'getLocation');
    ga.addParam('sysparm_desk', g_user.userID);
    ga.getXML(autoPopulate);

    function autoPopulate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer != '' && answer != g_form.getValue('desk location')) {
            g_form.setValue('desk location', answer);
        }

    }
   
}

Script Include:

 getLocation: function() {
        var loc;
        var dsk = this.getParameter('sysparm_desk');
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', dsk);
        gr.query();
        if (gr.next()) {
            return gr.u_desk_location.toString();
        }
            },

Please mark my answer correct if it helps you

 

 

Thank you for the reply!  I tested the above scripts and still nothing.  Instead of the logged-in user, it should be the on behalf of user field.

@appstorm 

Where is the on behalf of field ?on the incident form?