- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 08:43 PM
Hi All,
I have a reference field called "incident"on a custom table which refers to incident table.
Also I have a field called region which refers to cmn_location table in Incident table.
Another region field also present in custom table which value depends on the region of "Incident" selected.
I want to auto populate region field in custom table with the same value of region field with respect to
the Incident no. selected in the custom table. I have tried the below script but it's working fine but if I am choosing an "Incident" whose region field is blank ,In this case the region field in my custom table is not getting blank, at same time it returns the previous value which has been chosen, before choosing the blank value .
if (newValue == '') {
g_form.clearValue('u_region');
}
var ref = g_form.getReference('u_incident', callBackMethod);
}
function callBackMethod(ref) {
if (ref.u_region)
g_form.setValue('u_region', ref.u_region);
}
Please take a look at the two different incidents in the image attached for better understanding.
Thank you
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 09:49 PM - edited 02-13-2025 10:04 PM
Hello @1_DipikaD ,
The issue you're facing is that when you select an "Incident" with no region value, your custom table's region field is retaining the previous value instead of clearing out. You already have part of the solution with the g_form.clearValue('u_region') when the Incident field is cleared, but the callback function should also handle the case where the region field in the referenced Incident is blank.
Here's an updated version of the script that checks whether the Incident's region is null or blank and handles it appropriately:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
// Clear the region field if no Incident is selected or form is loading
g_form.clearValue('u_region');
return;
}
// Get reference to the selected Incident record
g_form.getReference('u_incident', function(ref) {
if (ref && ref.u_region) {
// If the Incident has a region, set it in the custom table
g_form.setValue('u_region', ref.u_region);
} else {
// If the Incident does not have a region, clear the custom table's region field
g_form.clearValue('u_region');
}
});
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards,
Kaustubh
If you found my response helpful, please mark it as correct and close the thread to benefit others.
Regards,
Kaustubh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 10:00 PM
try this -> clear the region if incident doesn't have it populated
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '') {
g_form.clearValue('u_region');
}
var ref = g_form.getReference('u_incident', callBackMethod);
}
function callBackMethod(ref) {
if (ref.u_region)
g_form.setValue('u_region', ref.u_region);
else
g_form.clearValue('u_region');
}
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
02-13-2025 09:49 PM - edited 02-13-2025 10:04 PM
Hello @1_DipikaD ,
The issue you're facing is that when you select an "Incident" with no region value, your custom table's region field is retaining the previous value instead of clearing out. You already have part of the solution with the g_form.clearValue('u_region') when the Incident field is cleared, but the callback function should also handle the case where the region field in the referenced Incident is blank.
Here's an updated version of the script that checks whether the Incident's region is null or blank and handles it appropriately:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
// Clear the region field if no Incident is selected or form is loading
g_form.clearValue('u_region');
return;
}
// Get reference to the selected Incident record
g_form.getReference('u_incident', function(ref) {
if (ref && ref.u_region) {
// If the Incident has a region, set it in the custom table
g_form.setValue('u_region', ref.u_region);
} else {
// If the Incident does not have a region, clear the custom table's region field
g_form.clearValue('u_region');
}
});
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards,
Kaustubh
If you found my response helpful, please mark it as correct and close the thread to benefit others.
Regards,
Kaustubh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 09:56 PM
Hello @1_DipikaD ,
Please try the below
// Triggered when Incident field value changes
g_form.onChange('u_incident', function() {
var incidentValue = g_form.getValue('u_incident');
// If the Incident field is blank, clear the region field
if (incidentValue == '') {
g_form.clearValue('u_region');
return; // Exit here, no need to proceed with callback
}
// Get the reference to the selected Incident and call the callback method
g_form.getReference('u_incident', function(ref) {
// If a region is set in the selected Incident, copy it to the custom table's region field
if (ref.u_region) {
g_form.setValue('u_region', ref.u_region);
} else {
// If the region is empty in the selected Incident, clear the custom region field
g_form.clearValue('u_region');
}
});
});
If this helps you please mark it as correct.
Regards,
Debasis
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 10:00 PM
try this -> clear the region if incident doesn't have it populated
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '') {
g_form.clearValue('u_region');
}
var ref = g_form.getReference('u_incident', callBackMethod);
}
function callBackMethod(ref) {
if (ref.u_region)
g_form.setValue('u_region', ref.u_region);
else
g_form.clearValue('u_region');
}
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
02-15-2025 07:05 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