-
-
Save sn-donbenjamin/89fe0cbea989d72d701c36ef88f517cc to your computer and use it in GitHub Desktop.
Revisions
-
iamwill revised this gist
Jun 11, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -108,7 +108,7 @@ isValidRecord() | Returns true if current record exists in the database. orderBy(String fieldName) | Order by fieldName ascending. orderByDesc(String fieldName) | Order by fieldName descending. ### addQuery operators, must be upper case :heavy_exclamation_mark: Operator | Type | Desc ---|---|--- -
iamwill revised this gist
Jun 11, 2019 . 1 changed file with 21 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ # GlideRecord & GlideAggregate Cheat Sheet :heavy_exclamation_mark: ## GlideRecord(String tableName) :heavy_exclamation_mark: ```javascript var gr = new GlideRecord('incident'); // use the incident table @@ -10,7 +10,7 @@ while (gr.next()) { // advance } ``` ### Dot walking 101 :heavy_exclamation_mark: GlideRecord provides access to fields via "Dot-walking", so when you query an incident you can access any field like this: @@ -29,7 +29,7 @@ gr.short_description | gr.getDisplayValue('short_description') gr.caller_id.name | gr.getDisplayValue('caller_id') gr.caller_id.manager.name | gr.caller_id.manager.getDisplayValue() ### getValue vs getDisplayValue :heavy_exclamation_mark: For the following field types, **value** and **display value** are different. @@ -51,7 +51,7 @@ gs.info(gr.caller_id.getDisplayValue()); // Jerrod Bennett **Important** Use .getDisplayValue() whenever showing a value in the UI, just in case a translation exists for that field value! ### Methods :heavy_exclamation_mark: Method | Description --- | --- @@ -140,7 +140,7 @@ while(gr.next()) { } ``` #### addEncodedQuery :heavy_exclamation_mark: ```javascript var gr = new GlideRecord('incident'); gr.addEncodedQuery("priority=1^ORpriority=2"); @@ -150,7 +150,7 @@ while(gr.next()) { } ``` #### addQuery :heavy_exclamation_mark: ```javascript var rec = new GlideRecord('incident'); rec.addQuery('active',true); @@ -163,7 +163,7 @@ while (rec.next()) { } ``` #### addQuery & addOrCondition :heavy_exclamation_mark: ```javascript var gr = new GlideRecord('incident'); var qc = gr.addNullQuery('assigned_to'); @@ -179,7 +179,7 @@ gr.addQuery('active','false'); // delete all inactive incidents gr.deleteMultiple(); ``` #### deleteRecord :heavy_exclamation_mark: ```javascript var gr = new GlideRecord('incident'); if (gr.get('99ebb4156fa831005be8883e6b3ee4b9')){ @@ -195,21 +195,21 @@ gs.info(gr.canDelete()); gs.info(gr.canRead()); ``` #### getDisplayValue :heavy_exclamation_mark: ```javascript var gr = new GlideRecord('incident'); gr.get('sys_id','ef43c6d40a0a0b5700c77f9bf387afe3'); gs.info(gr.getDisplayValue()); // INC0000050 ``` **For field** :heavy_exclamation_mark: ```javascript var gr = new GlideRecord('incident'); gr.get('sys_id','ef43c6d40a0a0b5700c77f9bf387afe3'); gs.info(gr.getDisplayValue("caller_id")); // Jerrod Bennett ``` #### getEncodedQuery :heavy_exclamation_mark: ```javascript var gr = new GlideRecord('incident'); gr.addQuery('active', true); @@ -226,21 +226,21 @@ gr.insert(); // insert without data in mandatory field gs.info(gr.getLastErrorMessage()); // Data Policy Exception: Short description is mandatory ``` #### getRecordClassName :heavy_exclamation_mark: ```javascript var gr = new GlideRecord("task"); gr.get("ef43c6d40a0a0b5700c77f9bf387afe3"); gs.info(gr.getRecordClassName()); // incident ``` #### getRowCount :heavy_exclamation_mark: ```javascript var gr = new GlideRecord('incident'); gr.query(); gs.info("Records in incident table: " + gr.getRowCount()); ``` #### insert :heavy_exclamation_mark: ```javascript var gr = new GlideRecord('incident'); gr.newRecord(); @@ -258,7 +258,7 @@ while (gr.next()) { } ``` #### setWorkflow :heavy_exclamation_mark: ```javascript var gr = new GlideRecord('incident'); gr.initialize(); @@ -270,23 +270,23 @@ for (var i=1;i<100;i++){ } ``` #### update :heavy_exclamation_mark: ```javascript var gr = new GlideRecord('incident'); gr.get('99ebb4156fa831005be8883e6b3ee4b9'); gr.short_description='Update the short description'; gr.update(); // Updates a single record ``` #### updateMultiple :heavy_exclamation_mark: ```javascript var gr = new GlideRecord('incident'); gr.addQuery('active', true); gr.setValue('state', 4); gr.updateMultiple(); // update the state of all active incidents to 4 - "Awaiting User Info" ``` #### isValid :heavy_exclamation_mark: ```javascript var gr = new GlideRecord('incident'); gs.info(gr.isValid()); // true @@ -304,7 +304,7 @@ gr.query(); // this retrieves latest 10 incident records created ``` ## GlideAggregate :heavy_exclamation_mark: Use GlideAggregate to easily run database aggregation (COUNT, SUM, MIN, MAX, AVG) queries. GlideAggregate extends GlideRecord! ```javascript @@ -325,7 +325,7 @@ Output: ``` ### Methods :heavy_exclamation_mark: Method | Description --- | --- -
iamwill revised this gist
Jun 6, 2019 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -33,6 +33,7 @@ gr.caller_id.manager.name | gr.caller_id.manager.getDisplayValue() For the following field types, **value** and **display value** are different. - Choice - Date & Time - Glide List - Journal -
iamwill revised this gist
Jun 5, 2019 . 1 changed file with 9 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,20 +12,22 @@ while (gr.next()) { // advance ### Dot walking 101 GlideRecord provides access to fields via "Dot-walking", so when you query an incident you can access any field like this: **gr.short_description** instead of **gr.getValue('short_description')** But it's best practice to save dot-walking for reference fields, like for getting a Caller's Company name: **gr.caller_id.company.name** or the Country of a Location of an Asset associated to an incident: **gr.cmdb_ci.location.country** Here is a table of dot-walking best practices: Avoid | Prefer --- | --- gr.sys_id | gr.getUniqueValue() gr.short_description | gr.getDisplayValue('short_description') gr.caller_id.name | gr.getDisplayValue('caller_id') gr.caller_id.manager.name | gr.caller_id.manager.getDisplayValue() ### getValue vs getDisplayValue -
iamwill revised this gist
Jun 5, 2019 . 1 changed file with 1 addition and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -117,7 +117,7 @@ Operator | Type | Desc <= | Number | Less than or equal to *num_val* = | String | Equals *val* != | String | Not Equals *val* IN | String | In Set of *val* e.g. gr.addQuery('number','IN','INC00001,INC00002') NOT IN | String | Not in Set of *val* STARTSWITH | String | Starts with *val* ENDSWITH | String | Ends with *val* @@ -168,17 +168,6 @@ qc.addOrCondition('assigned_to', 'javascript:gs.getUserID()'); gr.query(); // Get all incidents where unassigned OR assigned to me ``` #### deleteMultiple ```javascript -
iamwill revised this gist
Jun 5, 2019 . 1 changed file with 17 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -52,13 +52,16 @@ gs.info(gr.caller_id.getDisplayValue()); // Jerrod Bennett Method | Description --- | --- addActiveQuery() | Adds a filter to return active records. Returns GlideQueryCondition. addEncodedQuery(String query) | Adds an encoded query to other queries that may have been set. addNotNullQuery(String fieldName) | Adds a filter where fieldName values are not null. Returns GlideQueryCondition. addNullQuery(String fieldName) | Adds a filter where fieldName values are null. Returns GlideQueryCondition. addQuery(String fieldName, Object value) | Adds a filter where fieldName is equal to value. Returns GlideQueryCondition. addQuery(String fieldName, String operator, Object value) | Adds a filter. **See Operators** Returns GlideQueryCondition. **GlideQueryCondition** | Further refine a query condition addCondition(String fieldName, [optional String oper,] Object value) | Adds an AND condition to the current condition. addOrCondition(String fieldName, [optional String oper,] Object value) | Adds an OR condition to the current condition. **Has Access?** | canCreate() | Can the user create a record in this table? canDelete() | Can the user delete from this table? canRead() | Can the user read from this table? @@ -157,6 +160,14 @@ while (rec.next()) { } ``` #### addQuery & addOrCondition ```javascript var gr = new GlideRecord('incident'); var qc = gr.addNullQuery('assigned_to'); qc.addOrCondition('assigned_to', 'javascript:gs.getUserID()'); gr.query(); // Get all incidents where unassigned OR assigned to me ``` **Using the IN operator** ```javascript var gr = new GlideRecord('incident'); -
iamwill revised this gist
Jun 5, 2019 . 1 changed file with 31 additions and 34 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,9 +3,9 @@ ## GlideRecord(String tableName) ```javascript var gr = new GlideRecord('incident'); // use the incident table gr.query(); // fetch data from the database while (gr.next()) { // advance gs.info(gr.short_description); } ``` @@ -29,14 +29,15 @@ so `gs.info(gr.caller_id)` is the same as `gs.info(gr.get("caller_id").getValue( ### getValue vs getDisplayValue For the following field types, **value** and **display value** are different. - Date & Time - Glide List - Journal - Password - Price & Currency2 - Reference - Translated fields ```javascript var gr = new GlideRecord('incident'); @@ -73,7 +74,7 @@ getElement(String fieldName) | Returns the GlideElement for fieldName. getEncodedQuery() | Returns the current query condition as an encoded query string. getRecordClassName() | Returns the class name for the current record. getRowCount() | Returns the number of rows in the query result. getTableName() | Returns the name of the table used to instantiate GlideRecord. getUniqueValue() | Returns the primary key of the record, which is usually the sys_id. getValue(String fieldName) | Returns the value for fieldName. **Record Operations** | @@ -91,12 +92,12 @@ setLimit(int max) | Set the maximum number of records to fetch for the query. setValue(String fieldName, Object value) | Sets the value of fieldName. setWorkflow(Boolean b) | Enables or disables the running of business rules, script engines, and audit. update(String reason) | Save the GlideRecord changes to the database. Reason is saved to the audit record. updateMultiple() | Applies setValue() to every record in the table that match the current query. **Is valid?** | isActionAborted() | Checks to see if the current database action is to be aborted. isValid() | Returns true if current table exists. isValidField(String fieldName) | Returns true if fieldName exists in the database. isValidRecord() | Returns true if current record exists in the database. **Order by** | orderBy(String fieldName) | Order by fieldName ascending. orderByDesc(String fieldName) | Order by fieldName descending. @@ -127,17 +128,19 @@ INSTANCEOF | String | Record class is *val* or a subclass of *val* ```javascript var gr = new GlideRecord('incident'); gr.addActiveQuery(); gr.query(); // Get incidents where active=true while(gr.next()) { // do something.... } ``` #### addEncodedQuery ```javascript var gr = new GlideRecord('incident'); gr.addEncodedQuery("priority=1^ORpriority=2"); gr.query(); // Get incidents where priority = 1 or 2 while(gr.next()) { // do something.... } ``` @@ -146,7 +149,7 @@ while (gr.next()) { var rec = new GlideRecord('incident'); rec.addQuery('active',true); rec.addQuery('sys_created_on', ">", "2010-01-19 04:05:00"); rec.query(); // Get incidents where active = true and created after 2010-01-19 04:05:00 while (rec.next()) { rec.active = false; gs.info('Active incident ' + rec.number + ' closed'); @@ -160,23 +163,23 @@ var gr = new GlideRecord('incident'); gr.addQuery('number','IN','INC00001,INC00002'); gr.query(); while(gr.next()) { // do something.... } ``` #### deleteMultiple ```javascript var gr = new GlideRecord('incident'); gr.addQuery('active','false'); // delete all inactive incidents gr.deleteMultiple(); ``` #### deleteRecord ```javascript var gr = new GlideRecord('incident'); if (gr.get('99ebb4156fa831005be8883e6b3ee4b9')){ gr.deleteRecord(); } ``` @@ -224,12 +227,12 @@ gs.info(gr.getLastErrorMessage()); // Data Policy Exception: Short description i ```javascript var gr = new GlideRecord("task"); gr.get("ef43c6d40a0a0b5700c77f9bf387afe3"); gs.info(gr.getRecordClassName()); // incident ``` #### getRowCount ```javascript var gr = new GlideRecord('incident'); gr.query(); gs.info("Records in incident table: " + gr.getRowCount()); ``` @@ -266,26 +269,20 @@ for (var i=1;i<100;i++){ #### update ```javascript var gr = new GlideRecord('incident'); gr.get('99ebb4156fa831005be8883e6b3ee4b9'); gr.short_description='Update the short description'; gr.update(); // Updates a single record ``` #### updateMultiple ```javascript var gr = new GlideRecord('incident'); gr.addQuery('active', true); gr.setValue('state', 4); gr.updateMultiple(); // update the state of all active incidents to 4 - "Awaiting User Info" ``` #### isValid ```javascript var gr = new GlideRecord('incident'); -
iamwill revised this gist
Jun 5, 2019 . 1 changed file with 79 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # GlideRecord & GlideAggregate Cheat Sheet ## GlideRecord(String tableName) @@ -304,6 +304,84 @@ gr.query(); // this retrieves latest 10 incident records created ``` ## GlideAggregate Use GlideAggregate to easily run database aggregation (COUNT, SUM, MIN, MAX, AVG) queries. GlideAggregate extends GlideRecord! ```javascript var ga = new GlideAggregate('incident'); ga.addAggregate('COUNT', 'state'); ga.query(); while(ga.next()) { gs.info([ga.getDisplayValue('state'), ga.getAggregate('COUNT', 'state')]); } /* Output: New, 312 In Progress, 21 On Hold, 6 Closed, 28 */ ``` ### Methods Method | Description --- | --- addAggregate(String aggFn, String fieldName) | Add an aggregate function to fieldName. aggFn: (COUNT, MIN, MAX, SUM) addQuery(String fieldName, String operator, Object value) | Adds a filter. **See Operators** addTrend(String fieldName, String timeInterval) | Add timeInterval trend for fieldName. timeInterval: (Year, Quarter, Date, Week, DayOfWeek, Hour, Value) getAggregate(String aggFn, String fieldName) | Return the value of an aggregate function for fieldName groupBy(String fieldName) | Group by fieldName. orderByAggregate(String aggFn, String fieldName) | Orders the aggregate result based on the specified aggregate function and fieldName. ### Examples #### SUM with groupBy ```javascript var ga = new GlideAggregate("cmdb_ci"); ga.addAggregate('SUM', "cost"); // SUM the cost of every cmdb_ci ga.groupBy('sys_class_name'); // Group by record class name ga.orderByAggregate('SUM', 'cost'); // Order by the aggregate function ga.query(); while(ga.next()) { gs.info([ga.getDisplayValue('sys_class_name'), Math.floor(ga.getAggregate('SUM', 'cost'))]); } /* Output: Computer, 1511313 Linux Server, 182230 UNIX Server, 38399 Mass Storage Device, 34735 ... */ ``` #### addTrend ```javascript var ga = new GlideAggregate('incident'); ga.addAggregate('COUNT'); // Count all incidents opened each quarter ga.addTrend('opened_at', 'Quarter'); ga.query(); while(ga.next()) { gs.info([ga.getValue('timeref'), ga.getAggregate('COUNT')]); } /* Output: 3/2018, 9 4/2018, 2 1/2019, 38 2/2019, 310 */ ``` From: https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=c_GlideRecordScopedAPI -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -79,6 +79,7 @@ getValue(String fieldName) | Returns the value for fieldName. **Record Operations** | get(String value) | Fetch a record by primary key value, typically sys_id. get(Object fieldName, Object value) | Fetch a record where fieldName equals value. getLastErrorMessage() | Retrieves the last error message. hasNext() | Returns true if there are any more records in the GlideRecord object. initialize() | Creates a GlideRecord without any default values set. isNewRecord() | Returns true if the current record has not yet been inserted into the database. @@ -212,6 +213,13 @@ var encodedQuery = gr.getEncodedQuery(); gs.info(encodedQuery); // active=true^priority=1 ``` #### getLastErrorMessage ```javascript var gr = new GlideRecord('incident'); gr.insert(); // insert without data in mandatory field gs.info(gr.getLastErrorMessage()); // Data Policy Exception: Short description is mandatory ``` #### getRecordClassName ```javascript var gr = new GlideRecord("task"); -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -118,7 +118,7 @@ STARTSWITH | String | Starts with *val* ENDSWITH | String | Ends with *val* CONTAINS | String | Contains *val* DOES NOT CONTAIN | String | Does not contain *val* INSTANCEOF | String | Record class is *val* or a subclass of *val* ### Examples -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,7 +27,7 @@ When you dot-walk to a field you get the GlideElement for that field. So when yo so `gs.info(gr.caller_id)` is the same as `gs.info(gr.get("caller_id").getValue())` is the same as `gs.info(gr.getValue("caller_id"))` ### getValue vs getDisplayValue For the following field types, value and display value are different. -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 165 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,6 +27,26 @@ When you dot-walk to a field you get the GlideElement for that field. So when yo so `gs.info(gr.caller_id)` is the same as `gs.info(gr.get("caller_id").getValue())` is the same as `gs.info(gr.getValue("caller_id"))` #### getValue vs getDisplayValue For the following field types, value and display value are different. - Reference - Price & Currency2 - Glide List - Translated fields - Journal - Password ```javascript var gr = new GlideRecord('incident'); gr.get('sys_id','ef43c6d40a0a0b5700c77f9bf387afe3'); gs.info(gr.caller_id); // 5b7c200d0a640069006b3845b5d0fa7c gs.info(gr.caller_id.getDisplayValue()); // Jerrod Bennett ``` **Important** Use .getDisplayValue() whenever showing a value in the UI, just in case a translation exists for that field value! ### Methods Method | Description @@ -63,10 +83,12 @@ hasNext() | Returns true if there are any more records in the GlideRecord object initialize() | Creates a GlideRecord without any default values set. isNewRecord() | Returns true if the current record has not yet been inserted into the database. insert() | Inserts a new record. newRecord() | Creates a new record and sets the default values for the fields. next() | Moves to the next record in the GlideRecord object. query() | Perform the query. setLimit(int max) | Set the maximum number of records to fetch for the query. setValue(String fieldName, Object value) | Sets the value of fieldName. setWorkflow(Boolean b) | Enables or disables the running of business rules, script engines, and audit. update(String reason) | Save the GlideRecord changes to the database. Reason is saved to the audit record. updateMultiple() | Saves each GlideRecord in the list with any changes that have been made. **Is valid?** | @@ -100,14 +122,14 @@ INSTANCEOF | String | Record class is *val* ### Examples #### addActiveQuery ```javascript var gr = new GlideRecord('incident'); gr.addActiveQuery(); gr.query(); ``` #### addEncodedQuery ```javascript var queryString = "priority=1^ORpriority=2"; var gr = new GlideRecord('incident'); @@ -118,21 +140,160 @@ while (gr.next()) { } ``` #### addQuery ```javascript var rec = new GlideRecord('incident'); rec.addQuery('active',true); rec.addQuery('sys_created_on', ">", "2010-01-19 04:05:00"); rec.query(); while (rec.next()) { rec.active = false; gs.info('Active incident ' + rec.number + ' closed'); rec.update(); } ``` **Using the IN operator** ```javascript var gr = new GlideRecord('incident'); gr.addQuery('number','IN','INC00001,INC00002'); gr.query(); while(gr.next()) { //do something.... } ``` #### deleteMultiple ```javascript var gr = new GlideRecord('incident') gr.addQuery('active','false'); // delete all inactive incidents gr.deleteMultiple(); ``` #### deleteRecord ```javascript var gr = new GlideRecord('incident') if (gr.get('99ebb4156fa831005be8883e6b3ee4b9')) gr.deleteRecord(); } ``` #### canCreate, canDelete, canRead ```javascript var gr = new GlideRecord('incident'); gs.info(gr.canCreate()); gs.info(gr.canDelete()); gs.info(gr.canRead()); ``` #### getDisplayValue ```javascript var gr = new GlideRecord('incident'); gr.get('sys_id','ef43c6d40a0a0b5700c77f9bf387afe3'); gs.info(gr.getDisplayValue()); // INC0000050 ``` **For field** ```javascript var gr = new GlideRecord('incident'); gr.get('sys_id','ef43c6d40a0a0b5700c77f9bf387afe3'); gs.info(gr.getDisplayValue("caller_id")); // Jerrod Bennett ``` #### getEncodedQuery ```javascript var gr = new GlideRecord('incident'); gr.addQuery('active', true); gr.addQuery('priority', 1); gr.query(); var encodedQuery = gr.getEncodedQuery(); gs.info(encodedQuery); // active=true^priority=1 ``` #### getRecordClassName ```javascript var gr = new GlideRecord("task"); gr.get("ef43c6d40a0a0b5700c77f9bf387afe3"); gs.info(gr.getRecordClassName()); //incident ``` #### getRowCount ```javascript var gr = new GlideRecord('incident') gr.query(); gs.info("Records in incident table: " + gr.getRowCount()); ``` #### insert ```javascript var gr = new GlideRecord('incident'); gr.newRecord(); gr.name = 'New Incident'; gr.description = 'Incident description'; gr.insert(); // Returns new record sys_id ``` #### next ```javascript var gr = new GlideRecord('incident'); gr.query(); while (gr.next()) { gs.info([gr.number, gr.short_description, gr.caller_id.getDisplayValue()]); } ``` #### setWorkflow ```javascript var gr = new GlideRecord('incident'); gr.initialize(); gr.setWorkflow(false); // when false, runs almost immediately. When true, takes about 3 seconds. for (var i=1;i<100;i++){ gr.short_description = 'Sample incident ' + i; gr.description = 'Auto generated'; gr.insert(); } ``` #### update ```javascript var gr = new GlideRecord('incident') gr.get('99ebb4156fa831005be8883e6b3ee4b9'); gr.short_description='Update the short description'; gr.update(); ``` #### updateMultiple ```javascript var gr = new GlideRecord('incident') gr.addQuery('active', true); gr.query(); gr.setValue('state', 4); gr.updateMultiple(); // update the state of all active incidents to 4 - "Awaiting User Info" ``` #### ```javascript ``` #### isValid ```javascript var gr = new GlideRecord('incident'); gs.info(gr.isValid()); // true var anotherGr = new GlideRecord('wrong_table_name'); gs.info(anotherGr.isValid()); // false ``` #### setLimit ```javascript var gr = new GlideRecord('incident'); gr.orderByDesc('sys_created_on'); gr.setLimit(10); gr.query(); // this retrieves latest 10 incident records created ``` From: -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 3 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -78,8 +78,6 @@ isValidRecord() | Returns true if current record is valid. orderBy(String fieldName) | Order by fieldName ascending. orderByDesc(String fieldName) | Order by fieldName descending. ### addQuery operators, must be upper case Operator | Type | Desc @@ -104,9 +102,9 @@ INSTANCEOF | String | Record class is *val* #### addActiveQuery() ```javascript var gr = new GlideRecord('incident'); gr.addActiveQuery(); gr.query(); ``` #### addEncodedQuery() @@ -121,8 +119,6 @@ while (gr.next()) { ``` #### deleteMultiple() ```javascript var gr = new GlideRecord('incident') gr.addQuery('active','false'); // delete all inactive incidents -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,7 +24,7 @@ or the Country of a Location of an Asset associated to an incident: **gr.cmdb_ci When you dot-walk to a field you get the GlideElement for that field. So when you use a dot-walked field as a string, it's calling the toString() method on the appropriate GlideElement class for that field. so `gs.info(gr.caller_id)` is the same as `gs.info(gr.get("caller_id").getValue())` is the same as `gs.info(gr.getValue("caller_id"))` ### Methods -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 21 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,32 @@ # GlideRecord Cheat Sheet ## GlideRecord(String tableName) ```javascript var gr = new GlideRecord('incident'); gr.query(); while (gr.next()){ gs.info(gr.short_description); } ``` ### Dot walking 101 GlideRecord provides access to fields via "Dot-walking", so when you query for an incident you can use: **gr.short_description** instead of **gr.getValue('short_description')** and even better, you can dot-walk as deep as you want through Reference fields: Get a Caller's Company name: **gr.caller_id.company.name** or the Country of a Location of an Asset associated to an incident: **gr.cmdb_ci.location.country** When you dot-walk to a field you get the GlideElement for that field. So when you use a dot-walked field as a string, it's calling the toString() method on the appropriate GlideElement class for that field. so **gs.info(gr.caller_id)** is the same as **gs.info(gr.get("caller_id").getValue())** is the same as **gs.info(gr.getValue("caller_id"))** ### Methods Method | Description -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 19 additions and 23 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -60,29 +60,25 @@ orderByDesc(String fieldName) | Order by fieldName descending. ### addQuery operators, must be upper case Operator | Type | Desc ---|---|--- = | Number | Equals *num_val* != | Number | Not Equals *num_val* > | Number | Greater than *num_val* >= | Number | Greater than or equal to *num_val* < | Number | Less than *num_val* <= | Number | Less than or equal to *num_val* = | String | Equals *val* != | String | Not Equals *val* IN | String | In Set of *val* (e.g. 1234,2345,3456,4567) NOT IN | String | Not in Set of *val* STARTSWITH | String | Starts with *val* ENDSWITH | String | Ends with *val* CONTAINS | String | Contains *val* DOES NOT CONTAIN | String | Does not contain *val* INSTANCEOF | String | Record class is *val* ### Examples -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,7 +17,7 @@ addNotNullQuery(String fieldName) | Adds a filter where fieldName values are not addNullQuery(String fieldName) | Adds a filter where fieldName values are null. addQuery(String fieldName, Object value) | Adds a filter where fieldName is equal to value. addQuery(String fieldName, String operator, Object value) | Adds a filter. **See Operators** **Has Access?** canCreate() | Can the user create a record in this table? canDelete() | Can the user delete from this table? canRead() | Can the user read from this table? -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 36 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,11 +14,9 @@ Method | Description addActiveQuery() | Adds a filter to return active records. addEncodedQuery(String query) | Adds an encoded query to other queries that may have been set. addNotNullQuery(String fieldName) | Adds a filter where fieldName values are not null. addNullQuery(String fieldName) | Adds a filter where fieldName values are null. addQuery(String fieldName, Object value) | Adds a filter where fieldName is equal to value. addQuery(String fieldName, String operator, Object value) | Adds a filter. **See Operators** **Has Access?** | canCreate() | Can the user create a record in this table? canDelete() | Can the user delete from this table? @@ -27,6 +25,39 @@ canWrite() | Can the user write to this table? **Delete** | deleteMultiple() | Deletes multiple records that satisfy the query condition. deleteRecord() | Deletes the current record. **Get** | getAttribute(String fieldName) | Returns the dictionary attributes for fieldName. getDisplayValue() | Returns the display value for the current record. getDisplayValue(String fieldName) | Returns the display value for fieldName. getElement(String fieldName) | Returns the GlideElement for fieldName. getEncodedQuery() | Returns the current query condition as an encoded query string. getRecordClassName() | Returns the class name for the current record. getRowCount() | Returns the number of rows in the query result. getTableName() | Returns the name of the table associated with the GlideRecord. getUniqueValue() | Returns the primary key of the record, which is usually the sys_id. getValue(String fieldName) | Returns the value for fieldName. **Record Operations** | get(String value) | Fetch a record by primary key value, typically sys_id. get(Object fieldName, Object value) | Fetch a record where fieldName equals value. hasNext() | Returns true if there are any more records in the GlideRecord object. initialize() | Creates a GlideRecord without any default values set. isNewRecord() | Returns true if the current record has not yet been inserted into the database. insert() | Inserts a new record. next() | Moves to the next record in the GlideRecord object. query() | Perform the query. setLimit(int max) | Set the maximum number of records to fetch for the query. setValue(String fieldName, Object value) | Sets the value of fieldName. update(String reason) | Save the GlideRecord changes to the database. Reason is saved to the audit record. updateMultiple() | Saves each GlideRecord in the list with any changes that have been made. **Is valid?** | isActionAborted() | Checks to see if the current database action is to be aborted. isValid() | Returns true if current table is valid. isValidField(String fieldName) | Returns true if fieldName is valid. isValidRecord() | Returns true if current record is valid. **Order by** | orderBy(String fieldName) | Order by fieldName ascending. orderByDesc(String fieldName) | Order by fieldName descending. ### addQuery operators -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,6 +13,7 @@ Method | Description --- | --- addActiveQuery() | Adds a filter to return active records. addEncodedQuery(String query) | Adds an encoded query to other queries that may have been set. addNotNullQuery(String fieldName) | Adds a filter where fieldName values are not null. **Is Valid?** | isValid() | Check if current table is valid isEncodedQueryValid(String queryString) | Check if *queryString* is valid -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 57 additions and 43 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,55 +1,31 @@ # GlideRecord Cheat Sheet ## GlideRecord(String tableName) Creates an instance of the GlideRecord class for the specified table. ```javascript var gr = new GlideRecord('incident'); ``` ### Methods Method | Description --- | --- addActiveQuery() | Adds a filter to return active records. addEncodedQuery(String query) | Adds an encoded query to other queries that may have been set. **Is Valid?** | isValid() | Check if current table is valid isEncodedQueryValid(String queryString) | Check if *queryString* is valid isValidField(String columnName) | Check if *columnName* is valid isValidRecord() | Check if current record is valid **Has Access?** | canCreate() | Can the user create a record in this table? canDelete() | Can the user delete from this table? canRead() | Can the user read from this table? canWrite() | Can the user write to this table? **Delete** | deleteMultiple() | Deletes multiple records that satisfy the query condition. deleteRecord() | Deletes the current record. ### addQuery operators @@ -76,6 +52,44 @@ if (gr.get('99ebb4156fa831005be8883e6b3ee4b9')) - INSTANCEOF ### Examples #### addActiveQuery() ```javascript var inc = new GlideRecord('incident'); inc.addActiveQuery(); inc.query(); ``` #### addEncodedQuery() ```javascript var queryString = "priority=1^ORpriority=2"; var gr = new GlideRecord('incident'); gr.addEncodedQuery(queryString); gr.query(); while (gr.next()) { gs.addInfoMessage(gr.number); } ``` #### deleteMultiple() *Do not use deleteMultiple() on tables with currency fields. Always delete each record individually.* ```javascript var gr = new GlideRecord('incident') gr.addQuery('active','false'); // delete all inactive incidents gr.deleteMultiple(); ``` #### deleteRecord() ```javascript var gr = new GlideRecord('incident') if (gr.get('99ebb4156fa831005be8883e6b3ee4b9')) gr.deleteRecord(); } ``` From: -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -29,7 +29,6 @@ var gr = new GlideRecord('incident'); **.deleteMultiple()** Deletes multiple records that satisfy the query condition. *Do not use deleteMultiple() on tables with currency fields. Always delete each record individually.* @@ -42,6 +41,7 @@ gr.deleteMultiple(); ``` **.deleteRecord()** Deletes the current record. ```javascript @@ -58,10 +58,10 @@ if (gr.get('99ebb4156fa831005be8883e6b3ee4b9')) - = - != - > - >= - < - <= #### String fields: -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 7 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,16 +8,18 @@ var gr = new GlideRecord('incident'); ### Is Valid? | --- | --- .isValid() | Check if current table is valid .isEncodedQueryValid(String queryString) | Check if *queryString* is valid .isValidField(String columnName) | Check if *columnName* is valid .isValidRecord() | Check if current record is valid ### Has Access? | --- | --- .canCreate() | Can the user create a record in this table? .canDelete() | Can the user delete from this table? .canRead() | Can the user read from this table? .canWrite() | Can the user write to this table? @@ -27,9 +29,12 @@ var gr = new GlideRecord('incident'); **.deleteMultiple()** Deletes multiple records that satisfy the query condition. *Do not use deleteMultiple() on tables with currency fields. Always delete each record individually.* ```javascript var gr = new GlideRecord('incident') gr.addQuery('active','false'); // delete all inactive incidents -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 10 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,35 +7,26 @@ var gr = new GlideRecord('incident'); ### Is Valid? .isValid() | Check if current table is valid --- | --- .isEncodedQueryValid(String queryString) | Check if *queryString* is valid .isValidField(String columnName) | Check if *columnName* is valid .isValidRecord() | Check if current record is valid ### Has Access? .canCreate() | Can the user create a record in this table? --- | --- .canDelete() | Can the user delete from this table? .canRead() | Can the user read from this table? .canWrite() | Can the user write to this table? ### Deleting Records **.deleteMultiple()** Deletes multiple records that satisfy the query condition. *Do not use deleteMultiple() on tables with currency fields. Always delete each record individually.* -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ var gr = new GlideRecord('incident'); ### Is Valid? method | --- | --- .isValid() | Check if current table is valid .isEncodedQueryValid(String queryString) | Check if *queryString* is valid -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,9 +7,10 @@ var gr = new GlideRecord('incident'); ### Is Valid? method | desc --- | --- .isValid() | Check if current table is valid .isEncodedQueryValid(String queryString) | Check if *queryString* is valid - **.isValidField(String columnName)** Check if *columnName* is valid -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,9 +7,9 @@ var gr = new GlideRecord('incident'); ### Is Valid? --- | --- **.isValid()** | Check if current table is valid **.isEncodedQueryValid(String queryString)** | Check if *queryString* is valid - **.isValidField(String columnName)** Check if *columnName* is valid -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 2 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,11 +8,8 @@ var gr = new GlideRecord('incident'); ### Is Valid? | **.isValid()** | Check if current table is valid | | **.isEncodedQueryValid(String queryString)** | Check if *queryString* is valid | - **.isValidField(String columnName)** Check if *columnName* is valid -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 8 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,17 +8,17 @@ var gr = new GlideRecord('incident'); ### Is Valid? - **.isValid()** Check if current table is valid - **.isEncodedQueryValid(String queryString)** Check if *queryString* is valid - **.isValidField(String columnName)** Check if *columnName* is valid - **.isValidRecord()** Check if current record is valid ### Has Access? -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 57 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,15 +20,66 @@ Check if *columnName* is valid **.isValidRecord()** Check if current record is valid ### Has Access? **.canCreate()** Can the user create a record in this table? **.canDelete()** Can the user delete from this table? **.canRead()** Can the user read from this table? **.canWrite()** Can the user write to this table? ### Deleting Records **.deleteMultiple()** Deletes multiple records that satisfy the query condition. *Do not use deleteMultiple() on tables with currency fields. Always delete each record individually.* ```javascript var gr = new GlideRecord('incident') gr.addQuery('active','false'); // delete all inactive incidents gr.deleteMultiple(); ``` **.deleteRecord()** Deletes the current record. ```javascript var gr = new GlideRecord('incident') if (gr.get('99ebb4156fa831005be8883e6b3ee4b9')) gr.deleteRecord(); } ``` ### addQuery operators #### Number fields: - = - != - > - >= - < - <= #### String fields: - = - != - IN - NOT IN - STARTSWITH - ENDSWITH - CONTAINS - DOES NOT CONTAIN - INSTANCEOF -
iamwill revised this gist
Jun 4, 2019 . 1 changed file with 26 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,6 +6,32 @@ var gr = new GlideRecord('incident'); ``` ### Is Valid? **.isValid()** Check if current table is valid **.isEncodedQueryValid(String queryString)** Check if *queryString* is valid **.isValidField(String columnName)** Check if *columnName* is valid **.isValidRecord()** Check if current record is valid ### addQuery operators | Operator | type | description | |---|---|---| | = | number | equals | | != | number | not equal | | > | number | greater than | | >= | number | greater than or equal | From: https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=c_GlideRecordScopedAPI
NewerOlder