Help with updating a field through Glide Ajax

conanlloyd
Giga Guru

Greetings all.   I'm still learning my way around Glide Ajax and know there's probably a very simple mistake in my code here but I can't see it.   All I want to do is update 2 variables (location and department ) on my form when the requested for person changes.

Here's my Script Include named getRequestedForData:

var getRequestedForData = Class.create();  

getRequestedForData.prototype = Object.extendsObject(AbstractAjaxProcessor, {  

 

getUserData: function() {

try {  

var userId = this.getParameter('sysparm_id');  

var result = this.newItem('result');  

var array = [];  

var gr = new GlideRecord('sys_user');

  gr.addQuery('sys_id', this.getParameter('sysparm_id'));

while(gr.next()) {

  var object = {};

  object.id = gr.getUniqueValue();

  object.loc = gr.getValue('location.name');

  object.dept = gr.getValue('department.name');

  array.push(object);

  }

  var json = new JSON();

  var data = json.encode(array);

  return data;

  }

  catch(e) {

  gs.log("CDL: E- " + e.message);

  }

  },  

      type: 'getRequestedForData'  

});

And here's my Catalog Client Script, (OnChange based on the requeste_for variable):

function onChange(control, oldValue, newValue, isLoading) {

  if (newValue == '') {

  g_form.setValue('iops_brm_location', '');

  g_form.setVisable('iops_brm_location',false);

  g_form.setValue('iops_brm_department', '');

  g_form.setVisible('iops_brm_department',false);

  return;

  }

  //get Requested For Data

  var ga = new GlideAjax("getRequestedForData");

  ga.addParam('sysparm_name','getUserData');

  ga.addParam("sysparm_id",g_form.getValue('requested_for'));

  confirm("ReqFor: GA Started");

  ga.getXML(setReqForDetails);

  function setReqForDetails(response){

  var answer = response.responseXML.documentElement.getAttribute("answer");

  try{

  answer = answer.evalJSON();

  }

  catch(e){

  answer = JSON.parse(answer);

  }

  g_form.setValue('iops_brm_location', answer.loc);

  g_form.setReadOnly('iops_brm_location',true);

  g_form.setVisable('iops_brm_location',true);

  g_form.setValue('iops_brm_department', answer.dept);

  g_form.setReadOnly('iops_brm_department',true);

  g_form.setVisable('iops_brm_department',true);

  }

}

1 ACCEPTED SOLUTION

conanlloyd
Giga Guru

I managed to find the solution.   I was using an array for no good reason which complicated things. I removed the array and updated the script include.



Old, non-working, Script Include:


var getRequestedForData = Class.create();      


getRequestedForData.prototype = Object.extendsObject(AbstractAjaxProcessor, {      


     


getUserData: function() {  


try {      


var userId = this.getParameter('sysparm_id');      


var result = this.newItem('result');      


var array = [];      


var gr = new GlideRecord('sys_user');    


  gr.addQuery('sys_id', this.getParameter('sysparm_id'));  


while(gr.next()) {  


  var object = {};  


  object.id = gr.getUniqueValue();  


  object.loc = gr.getValue('location.name');  


  object.dept = gr.getValue('department.name');  


  array.push(object);  


  }  


  var json = new JSON();  


  var data = json.encode(array);  


  return data;  


  }  


  catch(e) {  


  gs.log("CDL: E- " + e.message);  


  }  


  },      


      type: 'getRequestedForData'      


});



New, working, Script Include:


var getRequestedForData = Class.create();


getRequestedForData.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  getUserData: function(name) {


  var iopsSysId = this.getParameter('sysparm_id');


  var gr = new GlideRecord('sys_user');


  gr.addQuery("sys_id", iopsSysId);


  gr.query();


  if (gr.next()) {


  var ga = {};


  ga.id = gr.sys_id + '';


  ga.loc = gr.location.name + '';


  ga.dept = gr.department.name + '';


  var json = new JSON();


  ga = json.encode(ga);


  return ga;


  }


  }


  });


View solution in original post

7 REPLIES 7

Shishir Srivast
Mega Sage

Please check if below changes helps,



in Client Script:


try{  


answer = JSON.parse(answer);  


}



In script Include:


object.loc = gr.getDisplayValue('location');  


object.dept = gr.getDisplayValue('department');


Ashutosh Munot1
Kilo Patron
Kilo Patron

HI Conan,



Can you tell me where are you working on this code i.e. Service portal or ESS.



Thank you,
Ashutosh


conanlloyd
Giga Guru

I managed to find the solution.   I was using an array for no good reason which complicated things. I removed the array and updated the script include.



Old, non-working, Script Include:


var getRequestedForData = Class.create();      


getRequestedForData.prototype = Object.extendsObject(AbstractAjaxProcessor, {      


     


getUserData: function() {  


try {      


var userId = this.getParameter('sysparm_id');      


var result = this.newItem('result');      


var array = [];      


var gr = new GlideRecord('sys_user');    


  gr.addQuery('sys_id', this.getParameter('sysparm_id'));  


while(gr.next()) {  


  var object = {};  


  object.id = gr.getUniqueValue();  


  object.loc = gr.getValue('location.name');  


  object.dept = gr.getValue('department.name');  


  array.push(object);  


  }  


  var json = new JSON();  


  var data = json.encode(array);  


  return data;  


  }  


  catch(e) {  


  gs.log("CDL: E- " + e.message);  


  }  


  },      


      type: 'getRequestedForData'      


});



New, working, Script Include:


var getRequestedForData = Class.create();


getRequestedForData.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  getUserData: function(name) {


  var iopsSysId = this.getParameter('sysparm_id');


  var gr = new GlideRecord('sys_user');


  gr.addQuery("sys_id", iopsSysId);


  gr.query();


  if (gr.next()) {


  var ga = {};


  ga.id = gr.sys_id + '';


  ga.loc = gr.location.name + '';


  ga.dept = gr.department.name + '';


  var json = new JSON();


  ga = json.encode(ga);


  return ga;


  }


  }


  });