g_form.getValue not working in a loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 04:33 AM
Hi there,
I have a loop that checks through variables and checks their status (true or false).
I have the following code and the getValue function is not working. It works for the first variable and then not for the others, even though I know it is cycling through them as the message appears for every variable, however, it is missing the getValue part of the message after it has looped through the first variable.
Code:
for (var x = 0; x < mandatory.length; x++) {
g_form.addInfoMessage('Value of ' + mandatory[x] + ' is ' + g_form.getValue(mandatory[x]));
}
Output:
Value of honey_mustard is true
Value of chilli is
Value of sour_cream_and_onion is
Value of mint is
Value of barbecue is
Value of mayo is
Has this happened to anyone else, and if so, how did you fix it?
Many thanks in advance,
G

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 04:36 AM
@gunishi Could you please post the entire script here. This will give us some context, and will help in faster resolution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 04:53 AM
Thank you for the response.
My code is adapted from this:
function onSubmit(){
//Set the mandatory checkbox variable names and total mandatory count here
var mandatoryVars = 'md,do,pa,pa_c,pt,pharm_d,aprn_cp,aprn_cnm,rn,lpn,ma,phd,lpc,msw,lcsw,ld,crna,mha';
var mandatoryCount = 4;
var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
if(!passed){
//Abort the submit
alert('You must select at least ' + mandatoryCount + ' options.');
return false;
}
}
function forceMandatoryCheckboxes(mandatory, count){
//Split the mandatory variable names into an array
mandatory = mandatory.split(',');
var answer = false;
var varFound = false;
var numTrue = 0;
//Check each variable in the array
for(x=0;x<mandatory.length;x++){
//Check to see if variable exists
if(g_form.getControl(mandatory[x])){
varFound = true;
//Check to see if variable is set to 'true'
if(g_form.getValue(mandatory[x]) == 'true'){
numTrue ++;
}
}
}
//If we didn't find any of the variables allow the submit
if(varFound == false){
answer = true;
}
if(numTrue<count){
answer = false;
}
//Return true or false
return answer;
}
It is not the exact same, but essentially the same structure.
Thank you for your help.
Kind regards,
G
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 04:37 AM
are you sure the mandatory variable which seems to be an array has correct variables inside it?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 04:39 AM
Yes I am sure as the correct variable names are printed in the output, just not the values. A bit stumped with this one!
G