- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 03:43 AM
Hi,
I need some help with my script below.
I'm trying to set a catalogue item variable cost_centre to the cost centre "SSER Southern Europe" and make it read only if the value of the variable selected in location variable has a location source of "Galileo"
Location variable is a Reference to cmn_location
Cost Centre is a Reference to cmn_cost_center
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// Check if the field is being loaded or cleared
if (isLoading || newValue === '') {
return;
}
// Get the value of the location field
var locationSysId = g_form.getValue('location');
// Get the location record
var location = new GlideRecord('cmn_location');
location.get(locationSysId);
// Get the value of the location_source field
var locationSource = location.getValue('cmn_location_source');
// If the location_source is Galileo, set the name of the cost center variable to "SSER Southern Europe"
if (locationSource === 'Galileo') {
var costCentreName = "SSER Southern Europe";
g_form.setValue('cost_centre', costCentreName);
g_form.setReadOnly('cost_centre', true);
} else {
// If the location_source is not Galileo, clear the value of the cost_centre variable and make it editable
g_form.setValue('cost_centre', '');
g_form.setReadOnly('cost_centre', false);
}
Solved! Go to Solution.
- Labels:
-
Customer Service Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 04:52 AM
are you sure you are comparing choice value then?
update as this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// Check if the field is being loaded or cleared
if (isLoading || newValue === '') {
return;
}
// Get the value of the location field
var locationRef = g_form.getReference('location', callBackMethod);
}
function callBackMethod(locationRef){
// Get the value of the location_source field
var locationSource = locationRef.cmn_location_source;
// If the location_source is Galileo, set the name of the cost center variable to "SSER Southern Europe"
if (locationSource === 'Galileo') {
var costCentreName = "SSER Southern Europe";
g_form.setValue('cost_centre', costCentreName);
g_form.setReadOnly('cost_centre', true);
} else {
// If the location_source is not Galileo, clear the value of the cost_centre variable and make it editable
g_form.setValue('cost_centre', '');
g_form.setReadOnly('cost_centre', false);
}
}
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 03:50 AM
it's not recommended to use GlideRecord in client script
I assume cmn_location_source field name is correct and holds the string value
try this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// Check if the field is being loaded or cleared
if (isLoading || newValue === '') {
return;
}
// Get the value of the location field
var locationRef = g_form.getReference('location', callBackMethod);
}
function callBackMethod(locationRef){
// Get the value of the location_source field
var locationSource = locationRef.getValue('cmn_location_source');
// If the location_source is Galileo, set the name of the cost center variable to "SSER Southern Europe"
if (locationSource === 'Galileo') {
var costCentreName = "SSER Southern Europe";
g_form.setValue('cost_centre', costCentreName);
g_form.setReadOnly('cost_centre', true);
} else {
// If the location_source is not Galileo, clear the value of the cost_centre variable and make it editable
g_form.setValue('cost_centre', '');
g_form.setReadOnly('cost_centre', false);
}
}
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
04-06-2023 03:59 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 04:30 AM
so it's not a string field. It's a reference field I guess.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 04:34 AM
the variable is a refrence field, but location source is a String field on the location table.