Client Script to Auto-Populate Field on Incident
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2022 02:42 PM
Stuck with an onChange client script that uses synchronous GlideAjax to auto-populate the field 'desk location' on behalf of.
Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('x_g_irm_dos_inc.DeskLocationUtils');
ga.addParam('sysparm_name', 'getLocation');
ga.addParam('sysparm_desk', g_form.getValue('sys_user'));
ga.getXML(autoPopulate);
function autoPopulate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer != '' && answer != g_form.getValue('desk location')) {
g_form.setValue('desk location', answer);
}
}
}
Script Include
DeskLocationUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDesk: function() {
var dsk = this.getParameter('sysparm_desk');
var gr = new GlideRecord('sys_user');
gr.addQuery('u_desk_location', dsk);
gr.query();
if (gr.next()) {
var desk = gr.sys_id;
}
return desk;
},
getLocation: function() {
var loc;
var dsk = this.getParameter('sysparm_desk');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', dsk);
gr.query();
if (gr.next()) {
u_desk_location = gr.u_desk_location;
}
return loc;
},
type: 'DeskLocationUtils'
});
Desk location is a string field on the Incident table named x_g_irm_dos_inc_desk_location, while the desk location field name on the user table is u_desk_location. Any help with the above script(s) is greatly appreciated!
Thank you!
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2022 07:06 AM
Hello,
please replace your scripts like below and if you want logged in user Sys_id i replaced with g_user.userID which i replaced below in your script
client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('x_g_irm_dos_inc.DeskLocationUtils');
ga.addParam('sysparm_name', 'getLocation');
ga.addParam('sysparm_desk', g_user.userID);
ga.getXML(autoPopulate);
function autoPopulate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer != '' && answer != g_form.getValue('desk location')) {
g_form.setValue('desk location', answer);
}
}
}
Script Include:
getLocation: function() {
var loc;
var dsk = this.getParameter('sysparm_desk');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', dsk);
gr.query();
if (gr.next()) {
return gr.u_desk_location.toString();
}
},
Please mark my answer correct if it helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2022 08:11 AM
Thank you for the reply! I tested the above scripts and still nothing. Instead of the logged-in user, it should be the on behalf of user field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2022 08:25 AM
Where is the on behalf of field ?on the incident form?