官术网_书友最值得收藏!

Introduction to scripting

ServiceNow uses JavaScript as a language for the majority of the scripts you can write or edit. If you already have a background in JavaScript, then you will certainly have a head start when it comes to writing your first scripts. The JavaScript engine used for writing scripts is updated as new versions of ServiceNow are released. The other language used is called Jelly. Not many people outside of the ServiceNow space are aware of Jelly, and it certainly wasn't a language I was familiar with before working with ServiceNow. Fortunately, Jelly is not used in the majority of scripts, and we'll cover how and where to use it in a later chapter.

Before you start writing your first script, it is important to check whether your goal can be achieved without using script. This should always be a consideration before you get started. By leveraging configurations in the form of UI policies, for example, it is possible to avoid customization all together.  

When scripting, it is important to think about what you want to achieve, and which records and what fields in the database you will need access to. The most common scripts will simply need information from the current record being displayed or updated, but more complex scripts may need to access data from several different tables. It is a good practice to ensure that the database and table structure will allow you to get all the data you need for your script from the current record before you start.

One other point to note when scripting is to make sure your code is well documented, including good comments and descriptions of what goals are to be achieved. While you are writing code, it can seem that what it does will be obvious, but coming back to the same code after some time, that often will not be the case. Maintaining ServiceNow instances is a big part of an administrator's job, and trying to do this with no comments in scripts can be a laborious task. I have come across many instances with insufficient comments and descriptions, and a lot of time is initially wasted on discovering how it all hangs together. Documenting your code will not only help you over time, but will also help any other administrators who need to maintain your scripts and instances.

For example, it is important to explain what each function does:

/*This function returns true or false based on the whether the input variable is 0
Inputs: input - integer
*/
function myFunction(input) {
if (input == 0) {
return true;
} else {
return false;
}
}

Most scripts will use the glide classes provided by ServiceNow, which we will discuss in the next chapter.

主站蜘蛛池模板: 瓦房店市| 阿荣旗| 安阳县| 房产| 石河子市| 丰宁| 通海县| 兴业县| 汝南县| 武城县| 农安县| 中阳县| 安康市| 苏州市| 黄龙县| 台州市| 两当县| 潮安县| 杭州市| 文山县| 安庆市| 石门县| 贵南县| 宁夏| 定兴县| 西畴县| 炉霍县| 西安市| 息烽县| 米脂县| 蒙自县| 米易县| 温泉县| 台中市| 布尔津县| 闵行区| 吴江市| 喀喇沁旗| 桦川县| 新民市| 神木县|