logo

NJP

#6 Technical Best Practices of Client Scripts in ServiceNow | Client Scripts in ServiceNow

Import · Dec 14, 2020 · video

[Music] please subscribe to my channel and click on the bell icon to get the regular updates of my channel and do not forget to like comment and share hello everyone welcome back to sas with servicenow this is part of short series of learning client scripts in servicenow in this video you will learn about technical best practices to be followed while creating client scripts in servicenow as you know client scripts runs on user's browser and an improper client script can impact the performance of any form in service now so it's very important for a developer or administrator to create client scripts in such a way which don't degrade the performance of the form it is very important that you should know when to use client scripts the perfect use of flying script is validation of data on the form before user saves the racket validation on the form improves the user experience because user will come to know if there are data issues before submitting the information available on the form for example date validation like start date should not be more than end date on the form now it is also important that client script should run in right order of execution on a particular form however you will not find order field on the form of client script racket so while you will create the client script you will not find out of the box this for this particular field available on client script form so what you can do you can add that field and define proper order to make sure they execute as per the sequence of different functionalities and close code in a function it is also very important that you need to put your client script code within a function now this will not cause any issues related to scope of variables by default all client scripts have pre-populated functions in which you have to write your code if i talk about we have different types of client scripts on change on submit when you select those types system will automatically populate function according to those types and the best way is you have to write the code whatever code you want to write for that client script it should be enclosed in that particular function if you will not use functions in multiple client scripts then form might show some unexpected behavior which will also be really hard to troubleshoot so if for example you have multiple client scripts and uh you have not enclosed uh your code in in function in that case you might get different kind of conflicts within different functions and your form might behave might behave in a different way which is not expected as per the functionality and in that case it will be also really hard to troubleshoot run necessary client scripts client scripts does not have any filter to trigger as on load and on change client scripts which will always run on the form so if i talk about onload and on change so you will always change a feed you will always maybe form will always be loaded that's for sure so in that case you have to make sure that you are putting the right conditions in the script to perform the action on the form but that's really important that you have to filter the condition you have to put some specific conditions as per the data available on the form so that that script whatever code you have it takes it check that conditions first and then run the script and i would say it's a really best practice to utilize the conditions otherwise you can't run the code every time when form is getting loaded so it's really i would say a best practice uh to to provide the condition in your script do not use glide record query it is always recommendable not to use glide record query in client script and if you have to perform server call from client then you can use get reference method and that is also with callback function now there are other apis as well like you can also use g underscore scratch pad or you can also use glide ajax to call server-side code and you want to fetch it on your client so you can use glide ajax g-underscore scratch pad as well minimize server lookup client scripts can use the data available on the form or data which is received retrieved from the server however you should always try to use data available on the form which can minimize server lookup which is i would say a little bit time consuming so if you want to fetch data from server then it will definitely take a little bit more time so as a best practice you should always see opportunity if you can just use the data which is available on the form so you can design your client script in such a way that it uses the data which is already available on the form rather than querying or doing a server call avoid global client scripts now when you select global in a table field so we have a table basically there's a table field while creating the client script so when you will select global in that table field then client script becomes global client script which basically allows client script to run on every table of the system and run on every form you have in your service now system now this will unnecessarily impact the performance and there is no benefit of running same script on every form even you don't need it even you're not able to achieve the goal by running that client script in all the forms so even if you have to run same client script on multiple tables as a best practice you should create that client script on base table and make it inherit it so that it can run on child table forms as well so if i give you the example of task table so if you have to run a client script of different type of task records maybe all type of task records so in that case do not use global option you should always create that client script on task table that's a base table avoid dome manipulation now this is also one of the important point in order to decrease the basically decrease or upgrade the performance not decrease i would say to upgrade the performance to improve the performance of the form that is avoid dom manipulation so dom is something document object model so you should always avoid using document object model in client script now you can access different elements of the pages and perform some manipulation however that is not recommended because you have this html page in that page you have different elements when i say different elements maybe it's a button maybe a header and maybe different fields those are all different elements we have in a page now if you want to maybe it can be some time that you have to perform some customization which you cannot achieve with the help of out of the box sometimes customer might also ask you to have that kind of requirement but in that case and then you might come to a conclusion that you have to create or change some dome elements ideally that is not recommendable because that will degrade the performance of your form if it is really business critical so i won't say that you cannot use it at all ideally it is not recommendable but if without using it you cannot achieve the goal and if that particular requirement is business critical then definitely you can take that decision but if i talk about overall perspective we should always ignore and avoid using dom manipulation use of display value in set value so when you use g underscore form dot set value while and that's that is something you use to set a value on the form on the client directly so in that case if you are setting that value in a reference field you have to make sure that you are using display value as well in the parameter so overall if i talk about parameter point of view g underscore form dot set value it takes the input basically if i talk about it takes the field as as a parameter it takes the value and display value as well so you should always use display value in the parameter as well when you are using g underscore form dot set value so these are major best practices technical best practices which you should follow while creating client scripts in servicenow system thanks for watching this video have a great day

View original source

https://www.youtube.com/watch?v=YIRXA9eQq8Q