Client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2025 03:49 AM
Hi All,
I have a requirement to make the location field blank and mandatory on alm_asset form on every change of state field.
Once the state value changes, the location field should be blank and mandatory, so that the user can fill the location manually and update the asset form.
I have written the onChange client script on State field but it is not working.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (newValue == 1 || newValue == 2 || newValue == 3 || newValue == 6 || newValue == 7 || newValue == 8 || newValue == 9 || newValue == 10 || newValue == 11) {
g_form.clearValue('location');
g_form.setMandatory('location', true);
g_form.addErrorMessage('Please fill the Location field before saving the record');
return false;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2025 03:59 AM
hi @roomawakar
The return false statement in the script is typically used in onSubmit client scripts to prevent form submission. In an onChange client script, it’s not needed.
Check if other scripts or UI policies are controlling the location field’s mandatory state or value, they could conflict with your script.
Try bellow script once and test:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// Exit if the form is loading or the new value is empty
if (isLoading || newValue === '') {
return;
}
// Define the states where location should be cleared and mandatory
var targetStates = ['1', '2', '3', '6', '7', '8', '9', '10', '11']; // Adjust based on your choice list values
// Check if the new state is in the target list
if (targetStates.indexOf(newValue) >= 0) {
g_form.clearValue('location'); // Clear the location field
g_form.setMandatory('location', true); // Make location mandatory
} else {
g_form.setMandatory('location', false); // Make location non-mandatory for other states
}
}
I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.
thank you
Rajesh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2025 03:59 AM
In condition try string instead of integer as below:
if (newValue == '1' || newValue == '2' || newValue == '3' || newValue == '6' || newValue == '7' || newValue == '8' || newValue == '9' || newValue == '10' || newValue == '11')
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2025 04:13 AM
try this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') return;
// Use an array for the states where location should be reset and required
var requiredStates = ['1','2','3','6','7','8','9','10','11'];
// Check if the new state is in the requiredStates array
var isRequired = requiredStates.indexOf(String(newValue)) !== -1;
// Always clear the location if required, and set mandatory accordingly
if (isRequired) {
g_form.clearValue('location');
g_form.setMandatory('location', true);
g_form.showFieldMsg('location', 'Please fill the Location field before saving the record', 'error');
} else {
g_form.setMandatory('location', false);
g_form.hideFieldMsg('location', true); // Remove any lingering messages
}
}
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
05-30-2025 04:18 AM
Hi @roomawakar ,
your script should work
just check if the fieldname on the client script is populated with your state field name
enhancement to the script
if it's onChange of state you can use
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (newValue) {
g_form.clearValue('location');
g_form.setMandatory('location', true);
g_form.addErrorMessage('Please fill the Location field before saving the record');
}
}
if the state changes to specific values
you can use the scripts shared by other members
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya