Is it possible to use OnChange client script at Onload?

Arishmit
Tera Contributor

I have a requirement and I want to execute a OnChange client script when the form loads. Is this possible? Any help would be much appreciated. 

1 ACCEPTED SOLUTION

Yes Buddy, It is possible to use OnChange Client script at OnLoad.

amitshishod_0-1738042726716.png

 


I am assuming that the task(For Onload and Onchange) is same, then you simply remove isLoading from line 2 and it will work same for load and change.

Please mark my response correct and helpful, if it helps you.
Please reply if any issue happens.

View solution in original post

7 REPLIES 7

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Arishmit 

 

No mate, you cant do that technically.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Yes Buddy, It is possible to use OnChange Client script at OnLoad.

amitshishod_0-1738042726716.png

 


I am assuming that the task(For Onload and Onchange) is same, then you simply remove isLoading from line 2 and it will work same for load and change.

Please mark my response correct and helpful, if it helps you.
Please reply if any issue happens.

PritamG
Mega Guru

yes, you can execute an OnChange client script when the form loads by programmatically triggering the onChange event. Use a g_form.setValue() followed by a g_form.getValue() to simulate a change in the field.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === oldValue) {
        return; 
    }
   
    console.log('Field changed:', newValue);
}


if (!g_form.isNewRecord()) { 
    var value = g_form.getValue('field_name'); 
    g_form.setValue('field_name', value); 
}

Viraj Hudlikar
Giga Sage

Hello @Arishmit 

 

Yes, you can do so.

Check the video https://youtu.be/uMGYjVVbHRo?si=fUsRNACc7kBrvuNN

https://youtu.be/PoTEa5ohLHo?si=WbnRKt-wUm0oGDzj

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Video demonstrate onchange client script in servicenow. In this video i have explained that how we can implement onchange client script in servicenow instance. And i have also displayed a demo that how onchange client script can behave as onload client script. Please visit below for servicenow ...