Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Unable to set value of checkbox using Catalog Client Script

hetaldoctor
Kilo Expert

Hi everyone

I am facing 2 issues

1. I am trying to set value of a checkbox using catalogclient script. But it is not working. I tried below options

g_form.setValue('x', true);

g_form.setValue('x', 'true');

g_form.setValue('x', 1);

g_form.setValue('x', '1');

2. I am setting checkbox to mandatory in a catalog ui policy. But that is also not working

Any ideas on this front will be welcome

 

Regards

Hetal

5 REPLIES 5

nayanawadhiya1
Kilo Sage

Hello Hetal,

 

1- g_form.setValue('x', true);

2- Using Catalog Client Script you can make as mandatory.

function onSubmit(){
    //Set the mandatory checkbox variable names and total mandatory count here
    var mandatoryVars = 'option1,option2,option3,option4';
    var mandatoryCount = 2;
    
    var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
    if(!passed){
        //Abort the submit
        var message = 'You must select at least ' + mandatoryCount + ' options.';
        g_form.addErrorMessage(message);
        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.hasField(mandatory[x])){
            varFound = true;
            //Check to see if variable is set to 'true'
            if(g_form.getValue(mandatory[x]) == 'true'){
                numTrue ++;
                //Exit the loop if we have reached required number of 'true'
                if(numTrue >= count){
                    answer = true;
                    break;
                }
            }
        }
    }
    //If we didn't find any of the variables allow the submit
    if(varFound == false){
        answer = true;
    }
    //Return true or false
    return answer;
}

 

Hi Nayan

 

This solution is in case where i want to validate on submit

 

In my case i want to make the checkbox mandatory (it works fine when i set the checkbox variable as mandatory in the variable definition itself) using a catalog ui policy

Regards

Hetal

You need to check them in the onsubmit script

 

if (g_form.getValue('x')==false || g_form.getValue('x')=='')

{

g_form.showFieldMsg('x','Please check the box','error');

return false;

}


Please mark this response as correct or helpful if it assisted you with your question.

Jotiram Yadav
Tera Expert

I have written below simple code in onLoad script and it is working.

function onLoad() {
//Type appropriate comment here, and begin script below
g_form.setValue('checkme',true);
}

similarly I have made this checkbox mandatory using UI policy. This is working too.

See screenshots.

find_real_file.png

 

find_real_file.png

 

Hope this helps.

Hit Like / helpful/ Answered as applicable.

Thanks,

Jotiram