Last active
November 20, 2023 00:44
-
-
Save mtcoffee/b88dc2f0a15b96dd3f0ddabc739dc9b0 to your computer and use it in GitHub Desktop.
ServiceNow Trivia Table Generator
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 characters
| //Instant Trivia Game with a simple background script. | |
| //This script will create a Trivia table in your CMDB and load it with trivia questions from the Open Trivia Database, opentdb.com | |
| var table_name = 'trivia', | |
| extends_table = 'cmdb_ci', | |
| fields = ['question', 'answer']; | |
| createTable(table_name, extends_table, fields); | |
| fetchAndInsertTriviaData(20); | |
| createBanner(table_name); | |
| //dropTableAndCleanUp(table_name); //optionally drop table and banner object | |
| function createTable(table_name, extends_table, fields) { | |
| var table = new TableUtils(table_name); | |
| gs.info('Does ' + table_name + ' already exist? ' + table.tableExists()); | |
| if (!table.tableExists()) { | |
| // Build a hash map to create string fields | |
| var attrs = new Packages.java.util.HashMap(); | |
| fields.forEach(function (field) { | |
| var ca = new GlideColumnAttributes(field); | |
| ca.setType("string"); | |
| ca.setUsePrefix(false); | |
| attrs.put(field, ca); | |
| gs.info(attrs); | |
| }); | |
| var tc = new GlideTableCreator(table_name, table_name); | |
| tc.setColumnAttributes(attrs); | |
| if (typeof extends_table !== 'undefined') { | |
| tc.setExtends(extends_table); | |
| } | |
| tc.update(); | |
| } | |
| } | |
| function fetchAndInsertTriviaData(amount) { | |
| var r = new sn_ws.RESTMessageV2(); | |
| r.setHttpMethod('GET'); | |
| r.setEndpoint('https://opentdb.com/api.php?amount=' + amount); | |
| var response = r.execute(); | |
| var responseBody = response.getBody(); | |
| var httpStatus = response.getStatusCode(); | |
| var parsedData = JSON.parse(responseBody); | |
| var recordGr = new GlideRecord('trivia'); | |
| parsedData.results.forEach(function(result) { | |
| recordGr.initialize(); | |
| recordGr.name = result.question; | |
| recordGr.question = result.question; | |
| recordGr.answer = result.correct_answer; | |
| recordGr.insert(); | |
| }); | |
| } | |
| function createBanner(tableName) { | |
| // Create list banner | |
| var rec = new GlideRecord('sys_db_object'); | |
| if (rec.get('name', tableName)) { | |
| var info = "if (!RP.isRelatedList()) { gs.addInfoMessage(\"<b style='font-size: 150%'> 💬 Open or hover the question for full details. Answers are in the answer field. Use Assignment Group to score. </b>\"); }"; | |
| var uiActionBanner = new GlideRecord('sys_ui_action'); | |
| uiActionBanner.initialize(); | |
| uiActionBanner.table = rec.name; | |
| uiActionBanner.name = 'list view info message'; | |
| uiActionBanner.active = 'true'; | |
| uiActionBanner.list_banner_button = 'true'; | |
| uiActionBanner.comments = 'Creative List Banner'; | |
| uiActionBanner.condition = info; | |
| uiActionBanner.insert(); | |
| } | |
| } | |
| function dropTableAndCleanUp(tableName) { | |
| // Drop the table | |
| var tu = new TableUtils(); | |
| tu.drop(tableName); | |
| // Clean up UI action banner | |
| var rec = new GlideRecord('sys_ui_action'); | |
| if (rec.get('table', tableName)) { | |
| rec.deleteRecord(); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment