How to do this , reducing Client scripts, Is it good idea ?

Sironi
Kilo Sage

Hi All,

Kindly help me on my concern, 

if we have multiple OnChange client scripts on single field on same table , there Reducing all client scripts into single OnChange is Good Idea ?

if reducing  is Good Idea , then why ServiceNow OOB has multiple scripts on single field on same table ?  , they should have designed in Single -OnChange script . why they didn't design in single OnChange script ?

Supriya25_0-1753679910819.png

Sironi_0-1753680826250.png

Sironi_1-1753680890272.png

Sironi_2-1753681012766.png

 

Please share your opinions and suggestions on this.



4 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Sironi 

yes it's recommended but sometimes each onChange is written to handle different use-case.

But if the script becomes too complex with multiple IF else then better to separate those out.

Developers might have created new onChange on state field without noticing there was already onChange.

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

Nikhil Bajaj9
Giga Sage

Hi @Sironi ,

 

Although there are multiple client scripts on same field but there requirements and codes are different. You can't put all conditions/requirement in one client script. It will be very messy to understand and very difficult to maintain code so code is always broken down into pieces (one requirement, one code) for better readability, maintenance.

I dont think clubbing multiple codes/requirements in one scrip tis possible and advisable.

 

If my answer helped you in anyways, please mark it solution accepted.

Regards,

Nikhil Bajaj

Please appreciate my efforts, help and support extended to you by clicking on – “Accept as Solution”; button under my answer. It will motivate me to help others as well.
Regards,
Nikhil Bajaj

View solution in original post

Mark Manders
Mega Patron

If you do everything with one client script, it will be very complex to add another requirement to it, because every single use case that is handled through that client script, will have to be tested again. 

It will be difficult to troubleshoot and when you are making Ajax calls it becomes impossible to follow. 

 

Breaking it down helps with making it understandable (the title of the client script already tells you what it does) and it is easily changed.

 

That being said: if it is handling the same use case, it should be in the same script. My experience is that often things are added instead of updated. Many 'developers' just add something to comply to certain requirements instead of checking what's already there. 

 

So you could probably combine some of those scripts, but make sure it stays easy to troubleshoot/read and also maintainable.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

View solution in original post

Hi @Sironi ,

 

If we got such reuqirements which can't be clubbed together so definitely we need to create separate scripts, we dont have any choice. It will not result in any performance issue. Even if it willdo we have any other choice, no then go ahed. Only thing we can keep in mind to follow best practices while writing our code/scripts.

 

Regards,

Nikhil Bajaj

Please appreciate my efforts, help and support extended to you by clicking on – “Accept as Solution”; button under my answer. It will motivate me to help others as well.
Regards,
Nikhil Bajaj

View solution in original post

16 REPLIES 16

Ankur Bawiskar
Tera Patron
Tera Patron

@Sironi 

yes it's recommended but sometimes each onChange is written to handle different use-case.

But if the script becomes too complex with multiple IF else then better to separate those out.

Developers might have created new onChange on state field without noticing there was already onChange.

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

Thanks for your reply and sharing your opinion.

@Sironi 

Glad to help.

Please close the thread by marking appropriate responses as correct so that it benefits future readers.

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

Nikhil Bajaj9
Giga Sage

Hi @Sironi ,

 

Although there are multiple client scripts on same field but there requirements and codes are different. You can't put all conditions/requirement in one client script. It will be very messy to understand and very difficult to maintain code so code is always broken down into pieces (one requirement, one code) for better readability, maintenance.

I dont think clubbing multiple codes/requirements in one scrip tis possible and advisable.

 

If my answer helped you in anyways, please mark it solution accepted.

Regards,

Nikhil Bajaj

Please appreciate my efforts, help and support extended to you by clicking on – “Accept as Solution”; button under my answer. It will motivate me to help others as well.
Regards,
Nikhil Bajaj