determine if value is not null on all fields

Brian Lancaster
Tera Sage

We have a request page where a user can request updates to the CMDB.   Since there is no real way to determine what variables should be mandatory is there a way write a onsubmit client script to loop thought the variables to verify at least one other field was filled out.?

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

The easiest thing to do would be to create an array in your client script of all the variable names, then loop through each of the names checking the value of the variable. If none have values return false.


View solution in original post

4 REPLIES 4

Brad Tilton
ServiceNow Employee
ServiceNow Employee

The easiest thing to do would be to create an array in your client script of all the variable names, then loop through each of the names checking the value of the variable. If none have values return false.


How would I create an array of all the variable name?   Is there a way to do this dynamically so if we add more variables I do not have to update the script?


Hi Brian,



I don't think there's a great way to 'detect' the variables on the item so an array of names would be easiest. What you could do would be to use glideajax and query the variables associated with that item from the server and get all the names back in an array. That gets a little more complex if you have some in and out of variable sets, but it's doable.


Brian Lancaster
Tera Sage

Here is the script I created to make this work if anybody else runs into this issue.



function onSubmit() {


  //Type appropriate comment here, and begin script below


        var reqtype = g_form.getValue('request_type');


        var variableName = [];


        var variableName = 'variable1,variable2,variable3';


        var variable = variableName.split(',');


        var length = variable.length;


        var a = 0;


        for (var i = 0; i<length;i++){


                  var currentVariable = variable[i];


                  var checkVariable = g_form.getValue(currentVariable);


                  if (checkVariable != ''){


                            a++;


                  }


        }


        if (a < 1){


                  alert('You must tell us what you want to update in the system you selected');


                  return false;


        }


}