When we can use on load script in on change why they given on load client script separately ?

KM SN
Tera Expert

Can any one give me business scenario where we need of both ??

4 REPLIES 4

PatrickMutchler
Tera Guru

@KM SN,

 

Generally speaking, you wouldn't need both for the same condition.

 

onLoad Client Scripts run when the form or page is loaded
onChange Client Scripts run when something specific gets changed AND when the form loads

 

That being said, to give an example of using both on the same field, let's use the Category field on the Incident form.  If I wanted to hide the Subcategory field until the Category field was populated, I could create an onLoad client script to accomplish this (NOTE:  A UI Policy would be a better way to go about this, but this is just an example).

 

Now, if I wanted to set a default value for that Subcategory field, based on what was selected for Category, I could create an onChange client script to accomplish that.

 

These are trivial examples, but I hope this helps.

 

If you found this reply useful, please mark it as helpful.  If it answered your question, please mark it as resolved.

I am asking examples what are the scenario's we will use on load client script in on change client script.

And i am expecting not exactly what are on load CS and on change CS rather I am looking for why we have separate on load CS when we can do same in On change client script?

Hi @KM SN ,

 

Suppose you want your script working for Caller's Manager field, for onLoad Caller field populates based on that form load and manager populates at the time of onLoad, now you can change the caller based on onchange you can populate caller's manager.

 

 

********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************

Hi @KM SN 

onLoad - onLoad Client Scripts execute script logic when forms are loaded. Use onLoad Client Scripts to manipulate a form's appearance or content. For example, setting field or form-level messages based on the presence of a value. Use onLoad Client Scripts sparingly as they impact form load times.

 

onChange - onChange Client Scripts execute script logic when a particular field's value changes. Use onChange Client Scripts to respond to field values of interest and to modify another field's value or attributes. For example, if the State field's value changes to Closed Complete, generate an alert and make the Description field mandatory.