how to pop up alert message and save the box checked when the form saves

raj765_32
Tera Contributor

hi can anyone help me with the code that i have written a code to display an message when the check box is checked and when clicked on ok let the check box checked and when clicked on cancel then uncheck the check box

 

what happens is that after i click on 'ok' and save the form the check box is getting unchecked:

 

here is the code:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

if (g_form.getValue('knowledge') == 'true') {
var answer = confirm('Are You Sure?');
if (!answer){
g_form.setValue('knowledge', true);
}
}
}
3 REPLIES 3

Sid_Takali
Kilo Patron
Kilo Patron

Hi @raj765_32 Try below code 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    if (g_form.getValue('knowledge') === 'true') {
        var answer = confirm('Are you sure you want to set this to true?');
        if (!answer) {
            g_form.setValue('knowledge', false);
        }
    }
}

 

Regards,

Sid

Community Alums
Not applicable

Hi @raj765_32 ,

The issue in your code is while setting the knowledge field you are setting it to true. which is not correct.

As Sid mentioned the code should work fine.

One correction to the code that we should consider is that while setting the value for setValue function please consider putting a string value as some other type can cause issue. See the below 

 

SanjayG_0-1720474757726.png

 

Here is an updated code-

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    if (g_form.getValue('knowledge') === 'true') {
        var answer = confirm('Are You Sure?');
        if (!answer) {
            g_form.setValue('knowledge', 'false');
        }
    }
}

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

Sanjay191
Tera Sage

Hey @raj765_32 

Make sure the type of the field will be correct as your script and try to apply the script