How to restrict the form to be submitted when the script condition matches and to retain oldValue.

1_DipikaD
Kilo Sage

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

 

 
21 REPLIES 21

@1_DipikaD 

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! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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
}

@1_DipikaD 

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! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@1_DipikaD 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Sarthak Kashyap
Mega Sage

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');
    }
}

SarthakKashyap_1-1762334438789.png

 

 

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;
    }
}

SarthakKashyap_0-1762334402267.png

 

Result: 

Once I'm trying to save it is showing me error 

SarthakKashyap_2-1762334485816.png


Please mark my answer correct and helpful if this works for you

Thanks and Regards,
Sarthak