Why is my script include client/ script not working?

cpinedatx94
Tera Expert

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
        });
    }
}
2 ACCEPTED SOLUTIONS

Robbie
Kilo Patron
Kilo Patron

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'
});

 

 

 

 

 

View solution in original post

Sandeep Rajput
Tera Patron
Tera Patron

@cpinedatx94 

 

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
        });
    }
}

View solution in original post

6 REPLIES 6

Robbie
Kilo Patron
Kilo Patron

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'
});

 

 

 

 

 

Sandeep Rajput
Tera Patron
Tera Patron

@cpinedatx94 

 

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
        });
    }
}