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

Brad Bowman
Kilo Patron
Kilo Patron

After seeing this, it's good to know I'll still have a job. 

1) It doesn't look like your Script Include has the Client callable box checked as there should be more on the second line to extend Ajax

2) You cannot use an argument in the function declaration to receive the value from the client, you need a line like this:

var recordSysId = this.getParameter('sysparm_record_sys_id');

3) Your GlideRecord table name is incorrect, try cmn_location

 

There may be more.  Go through this guide to understand the process, then add alerts and gs.info or gs.addInfoMessage lines to your scripts to troubleshoot

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

Which line are you referring to with #2?

 

My client callable check box was checked and i have corrected the GlideRecord table name.

@cpinedatx94 - Brad was pointing out that in your Script Include you are not passing or assigning the value from the Client Script. This is achieved by using the line:

 

var recordSysId = this.getParameter('sysparm_record_sys_id');

 

@cpinedatx94 - Have you seen my response above? I've tried and tested both Scripts on my PDI to verify they work. They should be good for you to copy and paste.

 

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

Yea i copied and pasted and it still does not appear to be working