Adding data from a custom table to a catalog item

Alan42
Tera Guru

I've created a custom table that will be cleared and updated every few weeks with employee vacation data.    I need to populate a field on a catalog item with a field named 'vacationbalance'.  I've created a script include named ‘VacationHoursFetcher’ that's testing out properly.    

 

Script include named ‘VacationHoursFetcher’:

 

var VacationHoursFetcher = Class.create();

VacationHoursFetcher.prototype = Object.extend(new AbstractAjaxProcessor(), {

    getVacationHours: function(email) {

        var hours = 0;

        var gr = new GlideRecord('x_atrr_canadian_0_canadian_vacation');

        gr.addQuery('emailid', email); // Ensure this is the correct column name

        gr.query();

        if (gr.next()) {

            hours = gr.getValue('vacationbalance'); // Ensure this field name is correct

        }

        return hours;

    },

    type: 'VacationHoursFetcher'

});

 

Verification Test :

 

var test = new VacationHoursFetcher();

var result = test.getVacationHours('*****@*******.com'); // Use the test email address

gs.info('Vacation hours for jdenchev@atlasroofing.com: ' + result); // Log the result

 

Verification Test Results

*** Script: Vacation hours for *****@*******.com: 81.33

 

That's the correct hours from the user field so its working properly.

 

My understanding is that I should use a catalog client script on the item but what I've come up with isn't working.    I'm using a read only, single line text variable named 'vacationbalance' which I think the script below should populate with the users data.    

 

function onLoad() {

    var userEmail = g_user.email;

 

    var ga = new GlideAjax('VacationHoursFetcher');

    ga.addParam('sysparm_name', 'getVacationHours');

    ga.addParam('email', userEmail); // Ensure this parameter matches what the Script Include expects

    ga.getXMLAnswer(function(response) {

        var availableHours = response.responseXML.documentElement.getAttribute('answer');

        g_form.setValue('vacationbalance', availableHours); // Ensure this field ID is correct

    });

}

 

There are no obvious errors on the console but the data isn't populating the 'vacationbalance' field for the user.   Can you see anything that would cause the values from the script include to not display?  The application is global, applies to a catalog item, type of OnLoad, and applies on a catalog view is checked.     

 

Alan42_1-1719599823375.png

 

 

 

1 ACCEPTED SOLUTION

Slava Savitsky
Giga Sage

I think "g_user" object does not have a property called "email". Check if the field would get populated if you hardcode an email address in your client script.

View solution in original post

7 REPLIES 7

Slava Savitsky
Giga Sage

Is the "Client callable" checkbox checked in the script include record?

Alan42
Tera Guru

It is.

 

Alan42_0-1719604935180.png

 

AshishKM
Kilo Patron
Kilo Patron

Hi @Alan42 , 

Update the UI Type = All and add an alert method for "availableHours" to check the return value on portal.

 

AshishKM_0-1719606370649.png

 

function onLoad() {
    var userEmail = g_user.email;
    var ga = new GlideAjax('VacationHoursFetcher');
    ga.addParam('sysparm_name', 'getVacationHours');
    ga.addParam('email', userEmail); // Ensure this parameter matches what the Script Include expects
    ga.getXMLAnswer(function(response) {
        var availableHours = response.responseXML.documentElement.getAttribute('answer');
        alert("availableHours-->"+availableHours);        
        g_form.setValue('vacationbalance', availableHours); // Ensure this field ID is correct
    });
}

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Slava Savitsky
Giga Sage

I think "g_user" object does not have a property called "email". Check if the field would get populated if you hardcode an email address in your client script.