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:55 AM
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;
}
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 05:38 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 07:01 AM
what are the type of variables? I suspect those variables are part of MRVS
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader