Last active
July 7, 2022 19:58
-
-
Save ovarn/0cc919ef2bc35aa3d9cc5b87726cb72f to your computer and use it in GitHub Desktop.
The snippet creates a catalog item with all possible variables types and orders the created item with dummy data.
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
| var varObj = { | |
| 1: { name: 'YES_NO', value: 'No' }, | |
| 2: { name: 'MULTI_LINE_TEXT', value: '1\n2' }, | |
| 3: { name: 'MULTIPLE_CHOICE', value: 'second' }, | |
| 4: { name: 'NUMERIC_SCALE', value: '2' }, // 0 - 5 by default | |
| 5: { name: 'SELECT_BOX', value: 'software' }, // Incident category | |
| 6: { name: 'SINGLE_LINE_TEXT', value: 'Dummy text' }, | |
| 7: { name: 'CHECKBOX', value: true }, | |
| 8: { name: 'REFERENCE', value: '57af7aec73d423002728660c4cf6a71c' }, // OOTB incident | |
| 9: { name: 'DATE', value: '2022-01-07' }, | |
| 10: { name: 'DATE_TIME', value: '2022-01-07 06:05:33' }, | |
| 11: { name: 'LABEL', value: '' }, | |
| 12: { name: 'BREAK', value: '' }, | |
| // 13 does not exist | |
| 14: { name: 'CUSTOM', value: '' }, | |
| 15: { name: 'UI_PAGE', value: '' }, | |
| 16: { name: 'WIDE_SINGLE_LINE_TEXT', value: 'Long long string...' }, | |
| 17: { name: 'CUSTOM_WITH_LABEL', value: '' }, | |
| 18: { name: 'LOOKUP_SELECT_BOX', value: '2' }, // In Progress incident state | |
| 19: { name: 'CONTAINER_START', value: '' }, | |
| 20: { name: 'CONTAINER_END', value: '' }, | |
| 21: { name: 'LIST_COLLECTOR', value: '9d385017c611228701d22104cc95c371,e8caedcbc0a80164017df472f39eaed1' }, // OOTB incidents | |
| 22: { name: 'LOOKUP_MULTIPLE_CHOICE', value: '3' }, // On Hold incident state | |
| 23: { name: 'HTML', value: '<h1>Hello World</h1>' }, | |
| 24: { name: 'CONTAINER_SPLIT', value: '' }, | |
| 25: { name: 'MASKED', value: 'password' }, // Any string | |
| 26: { name: 'EMAIL', value: '[email protected]' }, | |
| 27: { name: 'URL', value: 'https://google.com/search?q=ServiceNow' }, | |
| 28: { name: 'IP_ADDRESS', value: '1.1.1.1' }, | |
| 29: { name: 'DURATION', value: '1 01:01:01' }, | |
| // 30 does not exist | |
| 31: { name: 'REQUESTED_FOR', value: '6816f79cc0a8016401c5a33be04be441' }, // System administrator | |
| 32: { name: 'RICH_TEXT_LABEL', value: '' }, | |
| 33: { name: 'ATTACHMENT', value: '40f93e16c0a801130137c1f1bf538539' } // OOTB attachment | |
| }; | |
| var catItemID = createCatalogItem(); | |
| createVariables(catItemID); | |
| orderItem(catItemID); | |
| function createCatalogItem() { | |
| var catItemGR = new GlideRecord('sc_cat_item'); | |
| catItemGR.initialize(); | |
| catItemGR.setValue('name', 'Test item with all variables'); | |
| return catItemGR.insert(); | |
| } | |
| function getSkippedTypes() { | |
| var skippedTypes = []; | |
| for (var type in varObj) { | |
| var dummyValue = varObj[type].value; | |
| if (!dummyValue) { | |
| skippedTypes.push(type); | |
| } | |
| } | |
| return skippedTypes; | |
| } | |
| function createVariables(catItemID) { | |
| var skippedTypes = getSkippedTypes(); | |
| var choiceGR = new GlideRecord('sys_choice'); | |
| choiceGR.addQuery('name', 'question'); | |
| choiceGR.addQuery('element', 'type'); | |
| choiceGR.addQuery('inactive', false); | |
| choiceGR.addQuery('value', 'NOT IN', skippedTypes.join()); | |
| choiceGR.orderBy('sequence'); | |
| choiceGR.query(); | |
| while (choiceGR.next()) { | |
| var varType = choiceGR.getValue('value'); | |
| var varName = choiceGR.getValue('label'); | |
| var order = choiceGR.getValue('sequence'); | |
| var variableGR = new GlideRecord('item_option_new'); | |
| variableGR.initialize(); | |
| variableGR.setValue('cat_item', catItemID); | |
| variableGR.setValue('type', varType); | |
| variableGR.setValue('question_text', varName); | |
| variableGR.setValue('name', 'variable_' + varType); | |
| variableGR.setValue('order', order); | |
| if (varObj[varType].name == 'LIST_COLLECTOR') { | |
| variableGR.setValue('list_table', 'incident'); | |
| } else if (varObj[varType].name == 'LOOKUP_MULTIPLE_CHOICE') { | |
| variableGR.setValue('lookup_table', 'incident'); | |
| variableGR.setValue('lookup_value', 'state'); | |
| variableGR.setValue('lookup_unique', true); | |
| } else if (varObj[varType].name == 'LOOKUP_SELECT_BOX') { | |
| variableGR.setValue('lookup_table', 'incident'); | |
| variableGR.setValue('lookup_value', 'state'); | |
| variableGR.setValue('lookup_unique', true); | |
| } else if (varObj[varType].name == 'REFERENCE') { | |
| variableGR.setValue('reference', 'incident'); | |
| } else if (varObj[varType].name == 'SELECT_BOX') { | |
| variableGR.setValue('choice_table', 'incident'); | |
| variableGR.setValue('choice_field', 'category'); | |
| } | |
| var variableID = variableGR.insert(); | |
| if (varObj[varType].name == 'MULTIPLE_CHOICE') { | |
| var questionChoiceGR = new GlideRecord('question_choice'); | |
| questionChoiceGR.initialize(); | |
| questionChoiceGR.setValue('question', variableID); | |
| questionChoiceGR.setValue('text', 'First'); | |
| questionChoiceGR.setValue('value', 'first'); | |
| questionChoiceGR.setValue('order', '100'); | |
| questionChoiceGR.insert(); | |
| questionChoiceGR.initialize(); | |
| questionChoiceGR.setValue('question', variableID); | |
| questionChoiceGR.setValue('text', 'Second'); | |
| questionChoiceGR.setValue('value', 'second'); | |
| questionChoiceGR.setValue('order', '200'); | |
| questionChoiceGR.insert(); | |
| } | |
| } | |
| } | |
| function getVarDataObj() { | |
| var varDataObj = {}; | |
| for (var type in varObj) { | |
| var dummyValue = varObj[type].value; | |
| if (dummyValue) { | |
| varDataObj['variable_' + type] = dummyValue; | |
| } | |
| } | |
| return varDataObj; | |
| } | |
| function orderItem(catItemID) { | |
| var varDataObj = getVarDataObj(); | |
| var cart = new sn_sc.CartJS(); | |
| var request = { | |
| 'sysparm_id': catItemID, | |
| 'sysparm_quantity': '1', | |
| 'variables': varDataObj | |
| }; | |
| var cartDetails = cart.orderNow(request); | |
| gs.info(cartDetails); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment