- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 07:17 AM
I used chatgpt to hlep write a client script and script include.
I basically have 3 fields on a form that needs to be populated from data that is on a completely different table. Hence the script include.
var autoPopMei = Class.create();
autoPopMei.prototype = {
initialize: function() {
},
getData: function(recordSysId) {
var result = {};
var gr = new GlideRecord('cmn.location'); // Replace with your table name
if (gr.get(recordSysId)) {
result.field1 = gr.getValue('u_gsa'); // Replace with your field names rate
result.field2 = gr.getValue('u_first_last'); // Replace with your field names first last
result.field3 = gr.getValue('u_mie'); // Replace with your field names mie
// Add more fields as needed
}
return JSON.stringify(result);
},
type: 'autoPopMei'
};
and heres the client script
function onLoad() {
// Get the record Sys ID or some reference to use in the Script Include
var recordSysId = g_form.getValue('location'); // Replace with your reference field
if (recordSysId) {
var ga = new GlideAjax('autoPopMei');
ga.addParam('sysparm_name', 'getData');
ga.addParam('sysparm_record_sys_id', recordSysId);
ga.getXMLAnswer(function(response) {
var result = response.responseXML.documentElement.getAttribute("answer");
var data = JSON.parse(result);
// Populate form fields with the data
g_form.setValue('u_hotel_rate', data.field1); // Replace with your field names
g_form.setValue('u_first_and_last', data.field2); // Replace with your field names
g_form.setValue('u_expenses', data.field3); // Replace with your field names
// Add more fields as needed
});
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 08:22 AM - edited 05-16-2024 08:32 AM
Hi @cpinedatx94,
There are a few issues here. Please use the tried and tested Client Script and Script Include below.
Additionally, I'd recommend reviewing this handy guide with examples of how to use GlideAjax in the SN Docs Best Practices
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
Client Script:
function onLoad(){
var recordSysId = g_form.getValue('location');
var gaLocation = new GlideAjax('autoPopMei');
gaLocation.addParam('sysparm_name', 'getData');
gaLocation.addParam('sysparm_record_sys_id', recordSysId);
gaLocation.getXMLAnswer(getdata);
function getdata(response) {
var passedVal = JSON.parse(response);
g_form.setValue('comments', passedVal.field1);
// Add more fields as needed
//g_form.setValue('u_hotel_rate', data.field1); // Replace with your field names
//g_form.setValue('u_first_and_last', data.field2); // Replace with your field names
//g_form.setValue('u_expenses', data.field3); // Replace with your field names
}
}
Script Include (Make sure the 'Client callable' is checked / set to true)
var autoPopMei = Class.create();
autoPopMei.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getData: function(){
var recordSysId = this.getParameter('sysparm_record_sys_id');
var result = {};
// Best practive tip. Never a good idea to use the variable name gr
var grLocation = new GlideRecord('cmn_location'); // Replace with your table name
if (grLocation.get(recordSysId)) {
result.field1 = grLocation.getValue('city'); // Replace with the field name you want to pass back from the lookup tabvle - lcoation table
//result.field2 = grLocation.getValue('u_first_last'); // Replace with the field name you want to pass back from the lookup tabvle - lcoation table
//result.field3 = grLocation.getValue('u_mie'); // // Replace with the field name you want to pass back from the lookup tabvle - lcoation table
// Add more fields as needed
}
return JSON.stringify(result);
},
type: 'autoPopMei'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 07:03 PM
function onLoad() {
var recordSysId = g_form.getValue('location'); // Ensure 'location' is the correct field name
if (recordSysId) {
var gaLocation = new GlideAjax('x_g_rss_ssa_kiosk.autoPop516');
gaLocation.addParam('sysparm_name', 'getData');
gaLocation.addParam('sysparm_record_sys_id', recordSysId);
gaLocation.getXMLAnswer(function(response) {
//var answer = response.responseXML.documentElement.getAttribute("answer");
var passedVal = JSON.parse(response);
// Populate form fields with the data
//g_form.setValue('comments', passedVal.field1); // Populate 'comments' field
g_form.setValue('u_hotel_rate', passedVal.field1); // Replace with your field names
g_form.setValue('u_first_and_last', passedVal.field2); // Replace with your field names
g_form.setValue('u_expenses', passedVal.field3); // Replace with your field names
// Add more fields as needed
});
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 08:22 AM - edited 05-16-2024 08:32 AM
Hi @cpinedatx94,
There are a few issues here. Please use the tried and tested Client Script and Script Include below.
Additionally, I'd recommend reviewing this handy guide with examples of how to use GlideAjax in the SN Docs Best Practices
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
Client Script:
function onLoad(){
var recordSysId = g_form.getValue('location');
var gaLocation = new GlideAjax('autoPopMei');
gaLocation.addParam('sysparm_name', 'getData');
gaLocation.addParam('sysparm_record_sys_id', recordSysId);
gaLocation.getXMLAnswer(getdata);
function getdata(response) {
var passedVal = JSON.parse(response);
g_form.setValue('comments', passedVal.field1);
// Add more fields as needed
//g_form.setValue('u_hotel_rate', data.field1); // Replace with your field names
//g_form.setValue('u_first_and_last', data.field2); // Replace with your field names
//g_form.setValue('u_expenses', data.field3); // Replace with your field names
}
}
Script Include (Make sure the 'Client callable' is checked / set to true)
var autoPopMei = Class.create();
autoPopMei.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getData: function(){
var recordSysId = this.getParameter('sysparm_record_sys_id');
var result = {};
// Best practive tip. Never a good idea to use the variable name gr
var grLocation = new GlideRecord('cmn_location'); // Replace with your table name
if (grLocation.get(recordSysId)) {
result.field1 = grLocation.getValue('city'); // Replace with the field name you want to pass back from the lookup tabvle - lcoation table
//result.field2 = grLocation.getValue('u_first_last'); // Replace with the field name you want to pass back from the lookup tabvle - lcoation table
//result.field3 = grLocation.getValue('u_mie'); // // Replace with the field name you want to pass back from the lookup tabvle - lcoation table
// Add more fields as needed
}
return JSON.stringify(result);
},
type: 'autoPopMei'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 07:03 PM
function onLoad() {
var recordSysId = g_form.getValue('location'); // Ensure 'location' is the correct field name
if (recordSysId) {
var gaLocation = new GlideAjax('x_g_rss_ssa_kiosk.autoPop516');
gaLocation.addParam('sysparm_name', 'getData');
gaLocation.addParam('sysparm_record_sys_id', recordSysId);
gaLocation.getXMLAnswer(function(response) {
//var answer = response.responseXML.documentElement.getAttribute("answer");
var passedVal = JSON.parse(response);
// Populate form fields with the data
//g_form.setValue('comments', passedVal.field1); // Populate 'comments' field
g_form.setValue('u_hotel_rate', passedVal.field1); // Replace with your field names
g_form.setValue('u_first_and_last', passedVal.field2); // Replace with your field names
g_form.setValue('u_expenses', passedVal.field3); // Replace with your field names
// Add more fields as needed
});
}
}