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

Running background scripts

The simplest place to experiment with server-side scripting is perhaps the Background Scripts section in ServiceNow. This provides you with a large text box in which you place your code and a button saying Run Script, without any fuss, formatting, or further complexity. Outputs from your session during execution (such as log statements) are captured and presented to the screen. This, therefore, provides an excellent place to run server-side code where you need to monitor the results, where you can experiment, or where you need to run one-off scripts.

Navigate to Background Scripts by going to System Definition > Scripts (Background).

You will be presented with a large script box; however, before you get too enthusiastic, heed the warning shown. The reason that accessing background scripts requires elevated privileges is to remind you that some badly written code can, in fact, delete all your data. Never experiment in a live production instance!

Tip

Inefficient scripts get stuck in an infinite loop and can seriously impact performance or even cause outages or security breaches. Always test in a sandbox instance first. If you do get stuck, try going to https://<instance>.service-now.com/cancel_my_transaction.do, using the same browser session. The instance will attempt to halt execution of whatever it is doing in the other tab.

Let's run the standard Hello, World! as our first script:

gs.info('Hello, world!'); 

As the output, you should see the following:

*** Script: Hello, world! 

The function used is in this code is not a standard JavaScript command, but is part of the ServiceNow platform. gs stands for GlideSystem, which is a collection of useful functions and utilities. The info function of gs writes a message to the system log, and Background Scripts echoes it back. In Chapter 8, Securing Applications and Data, we will explore logging in ServiceNow in much more detail.

Note

The various functions of GlideSystem will be introduced throughout this book. For a detailed list of all its capabilities, refer to the product documentation: https://developer.servicenow.com/app.do#!/api_doc?v=geneva&type=server&scoped=true&to=class__scoped_glidesystem__geneva.

You must be wondering why it's called GlideSystem. Originally, the company now named ServiceNow was called GlideSoft, and they produced a platform called Glide. As the company and platform grew, they were both renamed to the more familiar monikers used today. You may see other references to Glide and GlideSoft, particularly in the less recent parts of the platform.

The gs object is one of several that are instantiated before the code is run. You will see many more as we journey through the ServiceNow platform. Which ones are available is dependent upon the context the code is running in.

主站蜘蛛池模板: 威宁| 乌鲁木齐县| 辽中县| 宕昌县| 五华县| 安新县| 新化县| 梨树县| 荔浦县| 伊春市| 酒泉市| 鹤岗市| 云阳县| 富裕县| 沅陵县| 平塘县| 勃利县| 山阴县| 大冶市| 长寿区| 洛川县| 滦南县| 太原市| 涞水县| 洮南市| 乌审旗| 西畴县| 独山县| 平原县| 隆子县| 松桃| 汉中市| 新巴尔虎左旗| 寻乌县| 启东市| 尼木县| 张家界市| 卫辉市| 山阳县| 昌乐县| 文登市|