How to get Values from Custom with Label Variable ? How to set limit of values and restrict submit

S Deva
Tera Contributor

There is a variable Custom with Lable for selecting multiple change requests. It should have at least 3 values before the submit form. 

 

I have added script : 

 

var Changes = g_form.getvalue("field_name").toString().split(',');

var changes_length = Changes.length;

if(changes_length>=3){

return true;

}

return false;

 

But this is always return false . How to achieve this can anyone please help ?

11 REPLIES 11

jaheerhattiwale
Mega Sage
Mega Sage

@S Deva It should be return false; in the if block. Please check below code:

 

function onSubmit() {
var changes = g_form.getValue('<FIELD NAME HERE>');
changes = changes.toString().split(",");

if (changes.length >= 3) {
g_form.addErrorMessage("Action aborted.");
return false;
}
}

 

Please mark as correct answer if this solves your issue

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Its not working. Actually the length of the values is coming as "0" . 

kiran Garide
Tera Contributor

Hi @S Deva , Did you get any solution for this? i am also working on similar requirement.

Nikhil Singh1
Tera Contributor

Hi @kiran Garide  @S Deva  Did you get the solution for this? I too have the same requirement.

jaheerhattiwale
Mega Sage
Mega Sage

@Nikhil Singh1 @S Deva 

100% working solution.

You must validate that on in the widget (which is used in custom with label) it self. Create a new field (boolean) to store the validation result.

 

Use the new field in the on submit client script to abort the action.

 

Note:

On widget, to sent the value in new field use below syntax

$scope.page.g_form.setValue("<NEW FIELD HERE>", "<true/false validation result>");

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023