- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2024 11:37 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2024 02:13 PM
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.
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2024 12:57 PM
Is the "Client callable" checkbox checked in the script include record?
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2024 01:02 PM
It is.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2024 01:29 PM
Hi @Alan42 ,
Update the UI Type = All and add an alert method for "availableHours" to check the return value on portal.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2024 02:13 PM
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.
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/