Populate Location Field onLoad - Request-By and onChange - Requested-For

appstorm
Tera Contributor

I am trying to call a Script Include, using two different Client Scripts - one onLoad and the other onChange.  The end result is to have the Requested-By location populate on load and have the location field change, if the Requested-For field changes.

Here is my code:

SI

var CheckLocation = Class.create();
CheckLocation.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

getLocation: function() {

var gr = new GlideRecord("sys_user");
gr.addQuery('sys_id', this.getParameter('sysparm_userID'));
gr.query();
if(gr.next()){
return gr.location.getDisplayValue();
}
},

type: 'CheckLocation'
});

CS (onLoad)

function onLoad() {
   //Type appropriate comment here, and begin script below

    var ga = new GlideAjax('CheckLocation');
    ga.addParam('sysparm_name', "getLocation");
    ga.addParam('sysparm_userID', g_form.getValue('requested_by'));
    ga.getXMLAnswer(function(answer){
       if(answer != ''){
       g_form.setValue('location', answer);
       }
    });
}

CS (onChange)

find_real_file.png

Finally, here is the default value for location:

gs.getUser().getRecord().getValue('location.parent');

Nothing seems to be working, although on page load - the location field is mandatory and it shows blank, but the asterisk is grey, as if a value is being returned.

Thoughts?

2 REPLIES 2

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi appstorm,

There's no need to have onLoad script because default value is being set.

 

onChange()

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
    var ga = new GlideAjax('CheckLocation');
    ga.addParam('sysparm_name', "getLocation");
    ga.addParam('sysparm_userID', newValue);
    ga.getXMLAnswer(function(answer){
       if(answer != ''){
       g_form.setValue('location', answer);
       }
    });
}

Script Include

var CheckLocation = Class.create();
CheckLocation.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getLocation: function() {

        var gr = new GlideRecord("sys_user");
        gr.addQuery('sys_id', this.getParameter('sysparm_userID'));
        gr.query();
        if (gr.next()) {
            return gr.location.getDisplayValue();
        }
    },
    type: 'CheckLocation '
});

 

Execution result:

find_real_file.png

Forgot, set the default value on location field to below.

javascript:gs.getUser().getRecord().getValue("location");