How to restrict the form to be submitted when the script condition matches and to retain oldValue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hi All,
I am passing a validation when the below script condition matches through onchange Client script. But at the same time I need to restrict user from submitting the form with if the value of the justification field is less than 60.Also if user does any changes to a existing value and try to submit a newValue of less than 60 character, it should not take and the justification field should should retain it's oldValue . I have tried below scripts, onChange is working as expected but not sure how to restrict user from submitting the form with incorrect value without impacting existing value of the field(If form is getting submitted with value length <60).
Please suggest me on this what should be best approach to achieve this ? Please let know if you nee more info .
Thank You
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
You’re on the right track! I understand the requirement like this!!
Requirements Breakdown
- Restrict form submission if Justification is < 60 characters and Lifecycle State = BUILD.
- If the user changes Justification to < 60 and tries to submit,
- Block the submission
- Revert the Justification field to its previous (old) value
Use onChange to Store oldValue:
var justificationOldValue = ''; // define globally
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) return;
var lifecycleState = g_form.getValue("u_lifecycle_state");
var minLength = 60;
// Store the last valid value globally
if (newValue.length >= minLength || lifecycleState != 'BUILD') {
justificationOldValue = newValue;
g_form.hideErrorBox('u_build_state_justification');
} else {
g_form.showErrorBox('u_build_state_justification', 'The field must contain at least ' + minLength + ' characters.');
}
}
Use onSubmit to Block and Revert:
function onSubmit() {
var lifecycleState = g_form.getValue("u_lifecycle_state");
var minLength = 60; var buildJustification = g_form.getValue('u_build_state_justification');
if (buildJustification.length < minLength && lifecycleState == 'BUILD') { g_form.showFieldMsg('u_build_state_justification', 'The field must contain at least ' + minLength + ' characters.', 'error'); // Revert to old value if available if (justificationOldValue) {
g_form.setValue('u_build_state_justification', justificationOldValue); }
return false; // BLOCK SUBMISSION
}
return true; // Allow submission if all checks pass
}
function onLoad() {
justificationOldValue = g_form.getValue('u_build_state_justification');
}
Client Scripts - ServiceNow Documentation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Is your IF statement within your onSubmit correct? The IF statement is checking if the actual length of the u_build_state_justification field is LESS than the minimum length, THEN it will set the value of that field to the incorrect value length.
If you are wanting to add the extra logic with an onSubmit Client Script, I would imagine you'd want to use a "return false" within your IF statement. If the actual length of the field is LESS than the minimum length, "return false" would prevent the record save from occurring.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hi @lpruit2 ,
I have tried below Before update BR with condition lifecycleState == 'BUILD' and Build State justification changes . It seems I achieved my target . Still I am testing in multiple cases Please let me know if this could have any major impact . I inactivated the OnSubmit client script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
you can stop form submission in onsubmit script
You should clear the field value and don't set it again as it will again trigger onchange script
function onSubmit() {
//Type appropriate comment here, and begin script below
var lifecycleState = g_form.getValue("u_lifecycle_state");
var minLength = 60;
var buildstatejustification = g_form.getValue('u_build_state_justification');
if (buildstatejustification.length < minLength && lifecycleState == 'BUILD') {
g_form.addErrorMessage('Build State Justification field must contain at least ' + minLength + 'characters.');
return false;
}
}
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
