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
then for your enhancement simply use onSubmit OR update the existing onChange client script
business rule not required for your enhancement.
💡 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
I was configuring through the below approach, let me know if this is suitable ? Is it fine to add var answer = ga.getXMLWait().documentElement.getAttribute('answer'); in client script ?
var MakeBuildStateJustificationMandatory = Class.create();
MakeBuildStateJustificationMandatory.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getTheCreateDate: function() {
var userName = this.getParameter('sysparm_user_name');
var gr_build = new GlideRecord('u_cmdb_ci_app_instance');
gr_build.addQuery('sys_created_onRELATIVEGE', 'month@ago@on');
gr_build.addQuery('u_created_by', userName);
gr_build.query();
if (gr_build.next()) {
gs.info("Created On: " + gr_build.sys_created_on);
return true;
} else {
return false;
}
},
isJustificationValid: function() {
try {
var justification = this.getParameter('sysparm_justification');
var minLength = 60;
// Check if justification text meets length criteria
if (justification && justification.length >= minLength)
return 'true';
else
return 'false';
} catch (ex) {
gs.error('Error in MakeBuildStateJustificationMandatory.isJustificationValid: ' + ex.message);
return 'false';
}
},
type: 'MakeBuildStateJustificationMandatory'
});
Client script :-
function onSubmit() {
var justification = g_form.getValue('build_state_justification');
var lifeCycleState = g_form.getValue('u_lifecycle_state');
// Run only when the lifecycle state is BUILD
if (lifeCycleState === 'BUILD' && g_form.isModified('build_state_justification')) {
var ga = new GlideAjax('MakeBuildStateJustificationMandatory');
ga.addParam('sysparm_name', 'isJustificationValid');
ga.addParam('sysparm_justification', justification);
var answer = ga.getXMLWait().documentElement.getAttribute('answer');
if (answer === 'false') {
g_form.showFieldMsg(
'build_state_justification',
'Build State Justification must contain at least 60 characters.',
'error'
);
return false; // stop submission
}
}
return true; // allow form submission
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Sorry but why you require GlideAjax with onSubmit if you can handle this simply using onSubmit?
the script include is simply checking the length logic which can be done directly in onSubmit.
If some developer created that script include + Ajax logic then that developer did a mistake and followed a bad practice.
if you show field message which is error you need not use return false as form submission will be stopped by system as there was error.
I also included g_form.hideFieldMsg() so that the error message is cleared and form will be allowed to submit if validation passes
function onSubmit() {
var justification = g_form.getValue('build_state_justification');
var lifeCycleState = g_form.getValue('u_lifecycle_state');
g_form.hideFieldMsg('build_state_justification');
// Run only when the lifecycle state is BUILD
if (lifeCycleState === 'BUILD' && g_form.isModified('build_state_justification')) {
var minLength = 60;
if (justification.length < minLength) {
g_form.showFieldMsg(
'build_state_justification',
'Build State Justification must contain at least 60 characters.',
'error'
);
}
}
}
💡 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @1_DipikaD ,
I tried your problem in my PDI and it is working fine, I tried on incident table you can add your own table and field name please check below script
On Change
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var lifecycleState = g_form.getValue("state");
var minLength = 60;
alert("newValue.length = " + newValue.length);
if (newValue.length < minLength && lifecycleState == '2') {
alert("Inside length if");
g_form.setMandatory('description', true);
g_form.showErrorBox('description', 'The field must contain at least ' + minLength + ' characters.');
} else {
alert("Inside length Else");
g_form.setMandatory('description', false);
g_form.hideErrorBox('description');
}
}
On Submit
function onSubmit() {
//Type appropriate comment here, and begin script below
var lifecycleState = g_form.getValue("state");
var minLength = 60;
var buildstatejustification = g_form.getValue('description');
if (buildstatejustification.length < minLength && lifecycleState == '2') {
// g_form.clearValue('description');
g_form.setValue('description', buildstatejustification);
g_form.setMandatory('description', true);
g_form.addErrorMessage('Build State Justification field must contain at least ' + minLength + 'characters.');
return false;
}
}
Result:
Once I'm trying to save it is showing me error
Please mark my answer correct and helpful if this works for you
Thanks and Regards,
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
My way.
Put this into a variable set so you can add to any items that need it
onLoad script
declare g_form.isError = [];
It will contain JSON objects where the key is the variable name and the value is true/false
Have a function in a Catalog Client Script (non isloated) called
updateIsError
The function looks at the g_form.isError array and determines if a key for that variable exists and makes it either True or False - depending on if there is an error or not.
g_form.isError = [{"project_start_date":true},{"project_end_date":false}];
Then an onSubmit that looks at the array g_form.isError and then simply adds a message to tell the user there are errors for any variable, and if so, stops the submission.
Means you write an onChange to give immediate feedback to the user, stop submission if required and do not duplicate your code.
Or failing that, create an onLoad Catalog Client Script (non isolated) that has a function you can call for each item of code you want to run (or a Catalog Client Script for each function). Call the relevant function via an onChange for immediate feedback and then in an onSubmit you call all the same functions and stop submission if required.
