Onload client script to load data from a table

prabhmeet
Giga Expert

Hi,

I have create an onload client script to load data from a table to the main catalog item form. If the application name matches in the table, I have to populate all field from the table to the main form.

Will i have to use Glideajax or there is any other way? Kindly help me on how to achieve this with an example code.

1 REPLY 1

Baala T
Mega Guru

Hi,

Yes we have to use GlideAjax to achieve this. Please check the sample code below.

Script Include

var ApplicationUtil = Class.create();
ApplicationUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getApplicationData: function () {
		var oResponse = {};

		var sAppName = this.getParameter('sysparm_application_name');

		//Fetch the data from table
		var grTable = new GlideRecord('<Table Name>'); // Change the table name
		grTable.addEncodedQuery('u_application_name='+sAppName); // change the filed name
		grTable.query();
		
		if (grTable.next()) {
			oResponse['Field_1'] = grTable.getValue('u_field_1'); // change the field names
			oResponse['Field_2'] = grTable.getValue('u_field_2');
			oResponse['Field_3'] = grTable.getValue('u_field_3');
			oResponse['Field_4'] = grTable.getValue('u_field_4');
			oResponse['Field_5'] = grTable.getValue('u_field_5');
		}

		return new global.JSON().encode(oResponse);
	},

	type: 'ApplicationUtil'
});

Client Script

function onLoad() {
    var ga = new GlideAjax('ApplicationUtil');
    ga.addParam('sysparm_name', 'getApplicationData');
    ga.addParam('sysparm_application_name', g_form.getValue('fld_application_name'));

    ga.getXML(processResponse);
}

function processResponse(response) {
	var res = response.responseXML.documentElement.getAttribute("answer");
	var oAppConfig = JSON.parse(res);

	//Set the values to the fields
	g_form.setValue('u_fld_1', oAppConfig.Field_1); // change the field names
	g_form.setValue('u_fld_2', oAppConfig.Field_2);
	g_form.setValue('u_fld_3', oAppConfig.Field_3);
	g_form.setValue('u_fld_4', oAppConfig.Field_4);
	g_form.setValue('u_fld_5', oAppConfig.Field_5);
}

 

Regards,
Bala T