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

Community Alums
Not applicable

Hello Suryansh,

What is type of field Date on catalog item?  Is it date type field?

 

Regards,

Akshay

Hello @Akshay Kangankar 

Thank you for responding and yes it is a date type field on both the catalog item and the table also.

Community Alums
Not applicable

Hello @Suryansh Verma ,

Thanks for the clarification. Your requirement can be achieved using onchange client script and script include. I have created one example, PFB screen shot.

Onchange client script - 

find_real_file.png

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.getXMLWait();
    var answer = ajax.getAnswer();

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


}

 

Script include - 

find_real_file.png

 

code - 

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('incident');
        gd.addQuery('caller_id', req);
        gd.addQuery('u_issue_since', date);
        gd.query();
        if (gd.next()) {

            var id = gd.short_description;
        }

        return id;

    }

});

 

Catalog form - 

find_real_file.png

 

Kindly note that I have added my variables for testing, please user your variable names correctly.

 

 

Regards,

Akshay Kangankar

Thanks, @Akshay Kangankar again for the suggested solution.

I have implemented the same but getting errors when selecting the date on the catalog item.

"There is a JavaScript error in your browser console"