Populate catalog item fields from the values stored in a custom table.

Suryansh Verma
Tera Contributor

I have a catalog item with a date field and another text field.

The requirement is that when a requestor opens the catalog item then based on the date field value it should fetch the values from another table that contains this data for the requestor.

 

for example, the table "ABC" has the following data stored.

Username = "Test User"

Date = 31-08-2022

ID = 123

so what is needed is that when the requestor "Test User" opens the catalog item and selects the date as 31-08-2022 the other fields on the catalog item should populate the values stored in the table ABC for this date and this user.

so if the requestor selects the date 31-08-2022 the field u_ID on the catalog item should auto-populate from the ID field in the ABC table.

Any help would be appreciated.

15 REPLIES 15

Share both the scripts here and not the screenshots.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Client Script:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var request = g_form.getValue('name');
    var date = g_form.getValue('date_day');
    //alert('date '+date + '  requested ' + request );
    var ajax = new GlideAjax('dateformat');
    ajax.addParam('sysparm_name', 'getdate');
    ajax.addParam('sysparm_req', request);
    ajax.addParam('sysparm_date', date);
    ajax.getXMLWait();
    var answer = ajax.getAnswer();

   // alert("current date" + answer);
    g_form.setValue('ssp_code', answer);


}

 

Script Include:

 

var dateformat = Class.create();
dateformat.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getdate: function() {
        var req = this.getParameter('sysparm_req');
        var date = this.getParameter('sysparm_date');
        //gs.log('date ' + date + '  requested ' + req);
        var gd = new GlideRecord('x_sosb_fico_time_c_recorded_on_entries');
        gd.addQuery('u_name', req);
        gd.addQuery('date_day', date);
        gd.query();
        if (gd.next()) {

            var id = gd.ssp_code;
        }

        return id;

    }

});

Hi,

both script include and client scripts are in same scope?

update as this

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	var request = g_form.getValue('name');
	var date = g_form.getValue('date_day');
	var ajax = new GlideAjax('dateformat');
	ajax.addParam('sysparm_name', 'getdate');
	ajax.addParam('sysparm_req', request);
	ajax.addParam('sysparm_date', date);
	ajax.getXMLAnswer(function(answer){
		g_form.setValue('ssp_code', answer);
	});

}

Script Include:

var dateformat = Class.create();
dateformat.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getdate: function() {
		var req = this.getParameter('sysparm_req');
		var date = this.getParameter('sysparm_date');
		var gd = new GlideRecord('x_sosb_fico_time_c_recorded_on_entries');
		gd.addQuery('u_name', req);
		gd.addQuery('date_day', date);
		gd.query();
		if (gd.next()) {
			var id = gd.ssp_code;
			return id;
		}
		return '';
	}

});

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Suryansh Verma 

can you share your complete script here?

Also what's the field type for Date/Day in your table?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

Hello Suryansh,

I have used getXMLWait(); that's why it's not working in the portal. Please use following updated code.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var request = g_form.getValue('requested_for');
    var date = g_form.getValue('date');
   // alert('date ' + date + '  requested ' + request);
    var ajax = new GlideAjax('dateformat');
    ajax.addParam('sysparm_name', 'getdate');
    ajax.addParam('sysparm_req', request);
    ajax.addParam('sysparm_date', date);
    ajax.getXMLAnswer(setAnswer);

    function setAnswer(answer) {
     //   alert("current date" + answer);
        g_form.setValue('id', answer);
    }

}

 

Regards,

Akshay