Script include for service catalog

UmeshGowda
Tera Contributor

Hello Team,

 

Am trying to auto populate user location in service catalog using script include method and client script, when ever am selecting username.

 

 

Below is the client script i have written:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
 
var ga = new GlideAjax('LocationDetails');
ga.addParam('sysparm_name','getLocation');
ga.addParam('sysparm_value',g_form.getValue('user_name'));
ga.getXML(test);
 
function test(res){
 
var answer = res.response.documnetElement.getAttribute("answer");
alert(answer);
 
}
   
}
 
Below is the script include i have written :
 
var LocationDetails = Class.create();
LocationDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
getLocation: function(){
 
var loc = this.getParameter('sysparm_value');
var x =new GlideRecord('sys_user');
x.addQuery('sys_id', loc);
x.query();
if(x.next()){
gs.log('location is ',+x.location);
return x.location;
}
 
},
    type: 'LocationDetails'
});
 
But unfortunately, when am selecting username it's not auto populating user location filed, can someone please suggest me.
8 REPLIES 8

try this

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
alert(g_form.getValue('user_name')); 
var ga = new GlideAjax('LocationDetails');
ga.addParam('sysparm_name','getLocation');
ga.addParam('sysparm_value',g_form.getValue('user_name'));
ga.getXML(test);
 
function test(res){
 
var answer = res.response.documnetElement.getAttribute("answer");
alert(answer);
 
}
   
}
-Anurag

Hi,

No still issue exist.

Comment this for testing, 

  if (isLoading || newValue == '') {
      return;
   }

Just trying to narrow down on the issue here.

 

Also Add steps how you are testing this

-Anurag

Vishal Birajdar
Giga Sage

Hi @UmeshGowda 

 

Can you try updating client script as below :

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
 
var ga = new GlideAjax('LocationDetails');
ga.addParam('sysparm_name','getLocation');
ga.addParam('sysparm_value',g_form.getValue('user_name'));
ga.getXMLAnswer(test);    // **use getXMLAnswer  
 
function test(answer){
 
var result = answer;   // get result
alert(result);
 
}
   
}
Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates