PDF generated using the open source mwlib toolkit. See http://code.pediapress.c
PDF generated using the open source mwlib toolkit. See http://code.pediapress.com/ for more information. PDF generated at: Sun, 01 Sep 2013 14:43:51 PST Scripting Guide Scripting for ServiceNow 1 Glide Glide Stack Overview Glide is an extensible Web 2.0 development platform written in Java that facilitates rapid development of forms-based workflow applications (work orders, trouble ticketing, and project management, for example). User Interface Stack Technology Map Java Packages Technologies Used User Interface (Browser) DHTML CSS JavaScript com.glide.ui com.glide.jelly GlideServlet Apache Jelly com.glide.script Business Rules Mozilla Rhino com.glide.db Persistence JDBC GlideServlet The primary driver of Glide, and the only servlet in the system, is found in GlideServlet.java. The GlideServlet: • Handles inbound action requests • Renders pages • Merges data with forms • Presents to user • Interfaces with script layer Business Rules • ECMA / JavaScript implementation based on Mozilla Rhino [1] • Interfaces with persistence layer using JDBC recordset interface • Merges persistence layer meta-data with data for easy correlation Persistence • Persistence means any store • RDBMS • LDAP • File system • Uniform access regardless of store type • Provides QUID and meta-data capabilities • Interfaces presented to callers Glide Stack 2 • RecordSet • TableDescriptor • ElementDescriptor Diagram References [1] http:/ / www. mozilla. org/ rhino/ Glide Script Objects 3 Glide Script Objects Note: This functionality requires a knowledge of Javascript. Overview There are several important objects that are exposed when writing business rules. These objects are created when your script is called. You may access objects using a shorthand notation. These include: • gs - GlideSystem most of the utility functions may be found in this object. • system - Same as 'gs' • current - Current GlideRecord (read/write) only available when writing business rules. • previous - Previous GlideRecord values (read only): only available when writing business rules. Some of the more important classes are documented along with the Glide Script Objects. They can be created and used within a script or business rule or returned by one or more of the Script Object methods. Glide provides full scripting support when writing business rules. Business rules are written in Javascript and have access to the Glide scripting objects. The majority of Glide functionality is accessed using JavaScript. Using GlideRecord to Query Tables Note: This functionality requires a knowledge of Javascript. Overview In order to query a table, first create a ServiceNow object for the table. This object is called a GlideRecord. To create a GlideRecord, create the following in script: var target = new GlideRecord('incident'); This creates a variable called target which is a GlideRecord object for the incident table. The only parameter needed is the name of the table to be accessed. To process all records from the incident table, add the following script: target.query(); // Issue the query to the database to get all records while (target.next()) { // add code here to process the incident record } This issues the query() to the database. Each call to next() would load the next record which you would process and do whatever you want to do. But that is not the common case. Most of the time you actually want to retrieve a specific record or a specific set of records, and you have some criteria (query conditions) that define the records you want to obtain. For example, say Using GlideRecord to Query Tables 4 you want to obtain all the incident records that have a priority value of 1. Here is the code that would accomplish that. var target = new GlideRecord('incident'); target.addQuery('priority', 1); target.query(); // Issue the query to the database to get relevant records while (target.next()) { // add code here to process the incident record } Notice in the above code we added the line target.addQuery('priority', 1);. This is indicating that you only want the records where the priority field is equal to 1. We assume that the majority of queries that you will want to do will be equality queries, queries where you want to find records where a field is equal to a value. Therefore we provide this format of the query and do not make you specify that you want an equals operation, we just assume it. However, lets say you wanted to find all incidents where the priority field is GREATER THAN 1. In that case you have to provide us with the operator that you want to apply to priority and this is done by providing the operator in the addQuery() request as is shown below. var target = new GlideRecord('incident'); target.addQuery('priority', '>', 1); target.query(); // Issue the query to the database to get relevant records while (target.next()) { // add code here to process the incident record } Available JavaScript Operators So that leads to the question of what type of operators are possible. The table below contains the operators that can be supplied to the addQuery() request. = Field must be equal to value supplied. addQuery('priority', '=', 1); > Field must be greater than value supplied addQuery('priority', '>', 1); < Field must be less than value supplied. addQuery('priority', '<', 3); >= Field must be equal or greater than value supplied addQuery('priority', '>=', 1); <= Field must be equal or less than value supplied. addQuery('priority', '<=', 3); != Field must not equal the value supplied. addQuery('priority', '!=', 1); STARTSWITH Field must start with the value supplied. The example shown on the right will get all records where the short_description field starts with the text Error. addQuery('short_description', 'STARTSWITH', 'Error'); CONTAINS Field must contain the value supplied somewhere in the text. The example shown on the right will get all records where the short_description field contains the text Error anywhere in the field. addQuery('short_description', 'CONTAINS', 'Error'); IN Takes a map of values that allows commas, and gathers a collection of records that meet some other requirement. Behaves as Select * from <table> where short_description IN ('Error'), which is identical to Select * from <table> where short_description='Error'. For example, to query all variable values that belong to a specific Activity, use the IN clause to query all Activities that are of that type, and store their sys_ids in a map, or comma-separated list. Then query the variable value table and supply this list of sys_ids. addQuery('short_description', 'IN', 'Error,Success,Failure'); Using GlideRecord to Query Tables 5 ENDSWITH Field must terminate with the value supplied. The example shown on the right will get all records where the short_description field ends with text Error. addQuery('short_description', 'ENDSWITH', 'Error'); DOES NOT CONTAIN Field must not have with the value supplied anywhere in the text. The example shown on the right will get all records where the short_description field does not have the word Error. addQuery('short_description', 'DOES NOT CONTAIN', 'Error'); NOT IN Takes a map of values that allows commas, and gathers a collection of records that meet some other requirement. Behaves as: Select * from <table> where short_description NOT IN ('Error'). addQuery('short_description', 'NOT IN', 'Error,Success,Failure'); INSTANCEOF Special operator that allows you to retrieve only records of a specified "class" for tables which are extended. For example when going after configuration items (cmdb_ci table) you many want to retrieve all configuration items that are have are classified as computers. The code to the right will do that. addQuery('sys_class_name', 'INSTANCEOF', 'cmdb_ci_computer'); There are also some special methods that can be used when you want to search for data that is NULL or NOT NULL. To search for all incidents where the short_description field has not been supplied (is null), use the following query: var target = new GlideRecord('incident'); target.addNullQuery('short_description'); target.query(); // Issue the query to the database to get all records while (target.next()) { // add code here to process the incident record } To find all incidents in which a short_description has been supplied, use the following query: var target = new GlideRecord('incident'); target.addNotNullQuery('short_description'); target.query(); // Issue the query to the database to get all records while (target.next()) { // add code here to process the incident record } GlideRecord query examples To view additional examples as well as additional query related methods, refer to GlideRecord Server. query var rec = new GlideRecord('incident'); rec.query(); while (rec.next()) { gs.print(rec.number + ' exists'); } Using GlideRecord to Query Tables 6 update var rec = new GlideRecord('incident'); rec.addQuery('active',true); rec.query(); while (rec.next()) { rec.active = false; gs.print('Active incident ' + rec.number = ' closed'); rec.update(); } insert var rec = new GlideRecord('incident'); rec.initialize(); rec.short_description = 'Network problem'; rec.caller_id.setDisplayValue('Joe Employee'); rec.insert(); delete var rec = new GlideRecord('incident'); rec.addQuery('active',false); rec.query(); while (rec.next()) { gs.print('Inactive incident ' + rec.number + ' deleted'); rec.deleteRecord(); } OR query For an example of how to do an 'OR' query see the Building an OR Query article. Getting a User Object 7 Getting a User Object Note: This functionality requires a knowledge of Javascript. Overview In a business rule or other server javascript, gs.getUser() returns a user object. The user object is ServiceNow's internal representation of the currently logged in user. It has a lot of information about the user, and a variety of utility uploads/Industriel/ scripting-guide.pdf
Documents similaires
-
20
-
0
-
0
Licence et utilisation
Gratuit pour un usage personnel Attribution requise- Détails
- Publié le Mar 16, 2022
- Catégorie Industry / Industr...
- Langue French
- Taille du fichier 1.2557MB