g_form.getValue not working in a loop

gunishi
Tera Guru

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

7 REPLIES 7

@gunishi 

try to give alert and see what came in that variable

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
alert(mandatory);
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;
}

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

 

I have already implemented this and can see all the variables are correct. It's just the getValue bit that isn't working. 

 

Kind regards, 

G

@gunishi 

what are the type of variables? I suspect those variables are part of MRVS

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader