Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

set field to mandatory after form submission

Momiji
Tera Contributor

Hi!

 

I'm having trouble with setting a field to mandatory after submitting the form. I made an onSubmit client script with the usual script:

 

function onSubmit() {
        g_form.setMandatory('field', true);
        return false;
}
 
I'm not sure if I understood its usage correctly. After filling-up the form and clicked 'Save', the field became mandatory right away. But the requirement was to make it mandatory only after being able to save/submit the form.
1 ACCEPTED SOLUTION

Ryan122
Tera Expert

Since you are using g_form and only need to set read-only on submitted forms, you may want to consider the isNewRecord module and running onLoad. 

 

function onLoad(){

  if(!g_form.isNewRecord()){

    g_form.setMandatory('field',true);

  }

}

View solution in original post

3 REPLIES 3

pavani_paluri
Tera Guru
Tera Guru

Hi @Momiji ,

The onSubmit() client script runs right before the form is submitted. If you return false, the form won't submit.

 

To make the field mandatory, please use an onload client script or on change with necessary conditions required.

 

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Pavani P

Ryan122
Tera Expert

Since you are using g_form and only need to set read-only on submitted forms, you may want to consider the isNewRecord module and running onLoad. 

 

function onLoad(){

  if(!g_form.isNewRecord()){

    g_form.setMandatory('field',true);

  }

}

Momiji
Tera Contributor

thanks @Ryan122! I used your script and it works!