- Mastering ServiceNow Scripting
- Andrew Kindred
- 688字
- 2021-06-24 19:08:40
GlideForm
I would say that GlideForm or g_form is the most used client-side class. It is mainly used for getting data from the fields on the form and setting values to those fields as well. We can also change elements of those fields using g_form.
We'll start by looking at how to get and set values from and into fields:
var stateValue = g_form.getValue('state');
The string value inside the speech marks is the database name for the field. Make sure you use the name rather than the label when getting a value using g_form. The getValue method essentially puts the value of a field into a variable for you. This then allows you to use that variable to check against other data or pass as a parameter into a function:
g_form.setValue('state', '6');
The setValue method will immediately set the value of the field on the screen to what the script dictates. In this example, presuming we are on the incident table, the state field would change to resolved. Remember, here, we are using the number 6 as that is the choice value for the resolved state in ServiceNow.
It is also worth noting that the value at this point will only have changed on the screen in front of the user. The field in the database will not be updated until the record itself is.
Using g_form, we can also change elements of the field itself and not just the value it holds. Let's have a look at how to make a field mandatory, show and hide a field, and make a field read-only. The best practice for these actions is to use a UI policy; however, the conditions in a UI policy are limited so sometimes we need to use a script to perform these actions.
We'll start by setting a field to mandatory:
g_form.setMandatory('short_description', true);
This example would set the short description field to mandatory. If we want to reverse this, we just need to change true to false in the line. This can be helpful when using UI actions to move through states of a record and to ensure certain fields are filled in before moving to the next stage. This change to the mandatory state of the field is just temporary; if the form is reloaded, then the field will go back to its original mandatory or non-mandatory state.
To change whether a field is visible, we can use the following code:
g_form.setDisplay('assigned_to', false);
This example will hide the assigned to field, but we can show it again by changing false to true in the line of script. This method isn't used that often as this action can usually be performed by a UI policy. This method will allow the space the field has left behind to be reclaimed by other fields. The setVisible method is very similar to the setDisplay method except a blank space is left where the field used to be, which tends to make setDisplay the better aesthetic choice.
The method for setting a field to be read-only is as follows:
g_form.setReadOnly('description', true);
The preceding example will set the description field to read-only, meaning the field cannot be edited. This will only be while the current form is loaded and will revert back to its original state of read-only or not when the form reloads.
There are also some useful bits of information that g_form can get for you as well. To begin with, let's see how you can get the unique value or sys_id of a record using g_form:
var sysID = g_form.getUniqueValue();
The example puts the sys_id of the record in the sysID variable. This can be useful if you want the sys_id value even before the record is saved.
We can also use code to check whether the record has been saved or not yet:
if (g_form.isNewRecord()) {
//Run script only for new records
}
This isNewRecord method allows us to write script for only new records or for only records that have already been saved. This can help you to decide whether to insert or update a record at the end of the script.
- 電氣自動化專業英語(第3版)
- Cinema 4D R13 Cookbook
- Getting Started with Oracle SOA B2B Integration:A Hands-On Tutorial
- 樂高創意機器人教程(中級 下冊 10~16歲) (青少年iCAN+創新創意實踐指導叢書)
- 數據庫原理與應用技術
- AWS Certified SysOps Administrator:Associate Guide
- 西門子S7-200 SMART PLC實例指導學與用
- 大數據驅動的設備健康預測及維護決策優化
- OpenStack Cloud Computing Cookbook
- 電氣控制與PLC原理及應用(歐姆龍機型)
- Hands-On Dashboard Development with QlikView
- INSTANT Adobe Story Starter
- 電腦上網入門
- 人工智能:智能人機交互
- Machine Learning with Spark(Second Edition)