Clietn script

anurag_b
Tera Contributor

Can we execute On Change client script while form load? if yes then how?

2 ACCEPTED SOLUTIONS

Brad Bowman
Kilo Patron
Kilo Patron

If your field or variable has a default value, or is populated via an onLoad script or script UI Policy, then the onChange script will run.

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@anurag_b 

when you use onChange client script there is a isLoading variable

If you wish your onChange should run when form loads and also when field/variable changes then you can add your code accordingly

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    // Check if the form is loading
    if (isLoading) {
        // Code to execute when the form is loading
        // For example, you can set a field value or perform other actions
        g_form.setValue('field_name', 'default_value');
        return;
    }

    // Code to execute when the field value changes
    if (newValue !== oldValue) {
        // Your onChange logic here
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

If your field or variable has a default value, or is populated via an onLoad script or script UI Policy, then the onChange script will run.

Ankur Bawiskar
Tera Patron
Tera Patron

@anurag_b 

when you use onChange client script there is a isLoading variable

If you wish your onChange should run when form loads and also when field/variable changes then you can add your code accordingly

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    // Check if the form is loading
    if (isLoading) {
        // Code to execute when the form is loading
        // For example, you can set a field value or perform other actions
        g_form.setValue('field_name', 'default_value');
        return;
    }

    // Code to execute when the field value changes
    if (newValue !== oldValue) {
        // Your onChange logic here
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

vishwajeet5550
Mega Guru

yes, we are able to change onLoad script runs on onChange client script removing onLoading parameter