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

Harish KM
Kilo Patron
Kilo Patron

Hi in your client script g_form.getValue('sys_user')) // in place of sys user put field name from incident table. For example x_g_irm_dos_inc_desk_location or newValue

Regards
Harish

Harish KM
Kilo Patron
Kilo Patron

 ga.addParam('sysparm_desk', g_form.getValue('sys_user')); // this will not work in your script because there is no field called sys_user on incident form. 

What is the field on incident form to get User sysid? is that called_id field?

Regards
Harish

Thank you for the reply!  The field that should trigger the auto-populate is 'on behalf of' x_g_irm_dos_inc_on_behalf_of.  The caller_id field in this case does not auto-populate, since the person submitting the request is different than the person submitting it for.  Thus, I am wondering since the on behalf of field doesn't exist on the sys_user table, how we can capture the sys_id of that user in the client script for Incident?

prashant8
Tera Expert

If you are doing it based on caller field it should be caller_id , in place of sys_user