Script to Auto Populate catalog field from another table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2023 09:37 AM
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
Script Include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2023 09:51 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2023 07:57 AM
Thank you for your response. However, this did not work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2023 10:00 AM
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.
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2023 07:57 AM
Thank you for your reply. However, this did not work.