How to auto- populate value from a table to a catalogue item field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 08:52 AM
Hello Team,
I have a requirement to auto populate a date from a table to my Catalogue Item field how can I achieve it, I am trying to do it via Script Include & onLoad client script however it's not working-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 09:14 AM - edited 06-03-2025 09:16 AM
Hi @Priya Singh 2 2 ,
Your script is not missing because you have issue with logic
1. your not suppose to use alert in Server side script meaning Script include
2 you cannot pass the parameter like this 'sys_id' instead you need to use 'sysparm_id'
try to use onchange client script for better results u_baseline_used variable
Client script :
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return; // Don't run on load or if the value is empty
}
var sysIdToSend = g_form.getValue(newValue);// hoping this is reference variable
var ga = new GlideAjax('getinchange'); // Name of your Script Include
ga.addParam('sysparm_name', 'getdate'); // Name of the function in your Script Include
ga.addParam('sysparm_id', sysIdToSend); // Pass the retrieved sys_id as a parameter
// Call the server and handle the response
ga.getXMLAnswer(function(response) {
var dateValue = response;
if (dateValue) {
g_form.setValue('start_date', dateValue); // <-- : Change 'start_date' to your actual target field name
} else {
g_form.setValue('start_date', ''); // Clear the field if no date was returned
g_form.showFieldMsg('start_date', 'Could not retrieve a valid start date.', 'error');
}
});
}
Script include : check client callable true
var getinchange = Class.create();
getinchange.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// This function will be called by sysparm_name='getdate'
getdate: function() {
var recordSysId = this.getParameter('sysparm_id'); // Get the sys_id passed from the client
var gr = new GlideRecord('incident');
if (gr.get(recordSysId)) {
var dateValue = gr.getValue('u_effective_start_date');
return dateValue;
}
// If the record is not found or the field is empty, return an empty string
// or a specific message to indicate failure.
gs.log('getinchange.getdate: Record with sys_id ' + recordSysId + ' not found or date field empty.');// you can check in logs
return '';
},
type: 'getinchange' /
});
If this information proves useful, kindly mark it as helpful or accepted solution.
Thanks,
BK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 09:24 AM
why not use auto populate feature in catalog item and no scripting will be required?
Simply create a reference variable pointing to incident table and another date or date/time variable and use this
Auto-populate a variable based on a reference type variable (Utah)
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2025 07:00 AM
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader