Script to Auto Populate catalog field from another table

KMNichols
Tera Contributor

I haven't used javascript in years and I am having an issue getting a script to work to auto populate a field on my catalog form.

 

Field I am trying to populate is u_bu using the u_employee_number field to look up the value on the u_dw_user table.

 

The script runs but does not populate the BU

 

Here are my scripts

 

Catalog Script

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var empno = g_form.getValue('u_employee_number');
var ga = new GlideAjax('PopulateBu');
ga.addParam('sysparm_name','SetBu');
ga.addParam('sysparm_getEmpno', g_form.getValue('empno'));
ga.getXML(parseUserResponse);
 
function parseUserResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var parser = JSON.parse(answer);
g_form.setValue('u_bu', parser.bu);
 
}  
}

 

 

Script Include

var PopulateBu = Class.create();
PopulateBu.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
 
SetBu: function(){
 
var dw_user = {};
 
var data = this.getParameter('sysparm_getEmpNo');
var grTable = new GlideRecord('u_dw_user');
grTable.addQuery('u_eecempno','data');
grTable.query();
if(grTable.next()){
 
dw_user = {     
'u_bu' :grTable.u_businessunitcode+" - "  +  grTable.u_businessunitname,
};  
}
return JSON.stringify(dw_user);
 
 
},
 
    type: 'PopulateBu'
});

 

 

4 REPLIES 4

Harsh_Deep
Giga Sage
Giga Sage

Hello @KMNichols ,

 

Please use this -

var PopulateBu = Class.create();
PopulateBu.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
 
SetBu: function(){
 
var dw_user = [];
 
var data = this.getParameter('sysparm_getEmpNo');
var grTable = new GlideRecord('u_dw_user');
grTable.addQuery('u_eecempno','data');
grTable.query();
if(grTable.next()){
 
dw_user.push('u_bu' :grTable.u_businessunitcode+" - "  +  grTable.u_businessunitname);
}
return dw_user;
 
 
},
 
    type: 'PopulateBu'
});

 

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

Thank you for your response.  However, this did not work.

Ethan Davies
Mega Sage
Mega Sage

In your Script Include you are converting your Object key name to a String using 'u_dw'. Remove the '' from the script and also remove the comma at the end of the line. You only need the comma if you have another key being set on the next line.

dw_user = {     
u_bu :grTable.u_businessunitcode+" - "  +  grTable.u_businessunitname
};  

 You have the same issue for your 'data' variable, remove the '' from it, you just need the variable name. Currently you are setting it to a String.

grTable.addQuery('u_eecempno',data);

If you want to make sure the value inside data is a String

grTable.addQuery('u_eecempno', String(data));

Also, consider adding .setLimit(1) to your query if you only want it to return a single record, it is best practice.

grTable.setLimit(1);

 Or use .get().

var grTable = new GlideRecord('u_dw_user');
if (grTable.get('u_eecempno', data)) {
    // Code that gets executed if a record is found.
}

 

Thank you for your reply.  However, this did not work.