Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Onload client script button not working

krishnanara
Tera Contributor

Hi,

In the below onload client script, I kept the button "clear flag" when we click on this button it should make the checkbox field  u_cirt_in_progress to false and should save the form and stay on the same page but right now it is taking me to the new record page after clicking and it is not setting the checkbox field  u_cirt_in_progress to false. Could you please provide the fix for this?

 
function onLoad() {

    if (g_form.getValue('u_cirt_in_progress') == 'true') {

         g_form.addErrorMessage(
            '⚠ This account in progress ' +
            '<button onclick="clearflag()">Clear Flag</button>'
        );

    }
}

function clearflag() {
g_form.setValue('u_cirt_in_progress, 'false');
g_form.save();
    return false;
}
2 REPLIES 2

Vishal Jaswal
Tera Sage

Hello @krishnanara 

 

You’re missing closing quote 

 

g_form.setValue('u_cirt_in_progress', 'false');


Correct html button syntax

 

<button type="button" onclick="clearflag(); return false;">Clear Flag</button>

 

 


Hope that helps!

Ankur Bawiskar
Tera Patron

@krishnanara 

not good practice to use g_form.save() in onLoad client script

there is issue in your syntax

try this

function onLoad() {
    if (g_form.getValue('u_cirt_in_progress') == 'true') {
        g_form.addErrorMessage(
            '⚠ This account in progress ' +
            '<button type="button" onclick="clearflag()">Clear Flag</button>'
        );
    }
}

function clearflag() {
    // Set checkbox to false
    g_form.setValue('u_cirt_in_progress', 'false');

    // Save the record and stay on the same form
    g_form.save();

    // Prevent any default link/button behavior
    return false;
}

💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏

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