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.

Need to though an error message

Naresh43
Tera Contributor

Hi All,

I have created a field called "text" as a yes/no kind of variable so, when user selects the text as yes then we i have through an error meassge.

i have created like this onChange client script field as text 

its not working as expected can someone help me here ..

 

var output = g_form.getValue('text');
   if(output=='YES'){
    g_form.addErrorMessage('error message)');
   }
 
Regards,
Naresh
   
2 ACCEPTED SOLUTIONS

Maddysunil
Kilo Sage

@Naresh43 

Please validate the backend name of your choices inside dictionary of 'text' field. I am assuming Yes as true and no as false. Below is the script:

 

function onChangeText() {
    var output = g_form.getValue('text');
    if (output == 'true') {
        g_form.addErrorMessage('Error message: You selected "Yes" for the text field.');
    } else {
        g_form.clearMessages(); // Clear any existing error messages
    }
}

 

  

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

 

View solution in original post

@Naresh43 

Form does'nt get submitted for onchage client script, Try using on submit client script.

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

 

View solution in original post

16 REPLIES 16

yad_achyut
Giga Guru

Hi @Naresh43 ,

if your field is yes/no then the backend value is specify a value of 0 (false) or 1 (true). So if it is on onchange client script  on the same variable you can directly use newValue to get the value of selected variable. other than you can try the following code:

var output = newValue
   if(output=='true' || output== 1 )
g_form.addInfoMessage("This is true");
}
else
{
    g_form.addErrorMessage('error message)');
   }

 

Deborah Brown L
Kilo Sage

Hi @Naresh43 ,

 

Please clear the variable value after the addinfomessage.

 

var output = g_form.getValue('text');
   if(output=='YES'){
    g_form.addErrorMessage('error message)');
g_from.clearValue('text');
   }
if that variable is mandatory and empty, then it wont allow to submit the form.
 
Regards,
Deborah Brown L

shikhatyagi
Kilo Sage

Hi @Naresh43 

 

As per your script, it will display only error message. But, if you want to restrict form submission then write On submit client script.

 

var output = g_form.getValue('text');
   if(output=='yes'){
    g_form.addErrorMessage('error message');
    return false;
   }
 

Please Mark this Helpful and Accepted Solution if it solves your issue.

 

Thanks & Regards,

Shikha Tyagi

Maddysunil
Kilo Sage

@Naresh43 

Please validate the backend name of your choices inside dictionary of 'text' field. I am assuming Yes as true and no as false. Below is the script:

 

function onChangeText() {
    var output = g_form.getValue('text');
    if (output == 'true') {
        g_form.addErrorMessage('Error message: You selected "Yes" for the text field.');
    } else {
        g_form.clearMessages(); // Clear any existing error messages
    }
}

 

  

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

 

Hello @Maddysunil 

 

like i have the above code now its throwing an error but if try to submit the form its submiting after that error also .

can you hlep me on this