GlideElement

GlideElement provides methods for dealing with the fields in a GlideRecord object. This class is one of the smaller ones in ServiceNow.

One very handy set of methods in this class is detecting changes to a field. This is very helpful in closing down records. Sometimes we may want to run some script on closure of the record, but not every time the record is updated. This is when the changesTo method can be used. We'll assume we are using an incident, in this case where the closed state is 7:

if  (current.state.changesTo('7')) {
//Run some closure script
}

This preceding example will allow for some script to run when the record is closed, but only when it moves to being closed. If the closed record is subsequently updated, then this script will not run again. That is why this method is so helpful. You can also use changes and changesFrom methods as part of this set.

You may or may not be aware of what current refers to at this point, but we will take a closer look at this when we look at business rules in Chapter 5, Introduction to Server-Side Scripting.

Another helpful set of methods in GlideElement is checking whether a user is able to create, read, or write to records. We can use this to see whether a user should be able to perform these actions, which can be very helpful in UI action conditions.

Let's see how it works:

if (current.canCreate()) {
//Run some creation script for the current record type
}

This will check whether the logged-in user is able to create a record of the current type (incident, change request, and so on) and, if so, then run the script inside the if statement. We can also use the canRead and canWrite methods in a similar way.

With GlideElement being a smaller class, it is not as well used as some of the other server-side classes, but some of its methods are very helpful, particularly in UI actions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.29.145