|
| 1 | +// Scenario : As a ServiceNow Admin or Developer managing dozens of similar request forms (like “Request Laptop”, “Request Mobile”, “Request Access”, etc.). |
| 2 | +// Manually creating each catalog item is repetitive. |
| 3 | + |
| 4 | +// This code will Automate Catalog Item Creation with a Single REST Call |
| 5 | +//Script: POST /api/x_demo/catalog_creator/create |
| 6 | + |
| 7 | +(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) { |
| 8 | + |
| 9 | + var body = request.body.data; |
| 10 | + var result = {}; |
| 11 | + |
| 12 | + try { |
| 13 | + // 1. Create Catalog Item |
| 14 | + var catItem = new GlideRecord('sc_cat_item'); |
| 15 | + catItem.initialize(); |
| 16 | + catItem.name = body.name; |
| 17 | + catItem.short_description = body.short_description || ''; |
| 18 | + catItem.description = body.description || ''; |
| 19 | + catItem.category = getCategorySysId(body.category); |
| 20 | + catItem.owning_group = getOwner(body.owner); |
| 21 | + catItem.active = true; |
| 22 | + var catSysId = catItem.insert(); |
| 23 | + |
| 24 | + result.catalog_sys_id = catSysId; |
| 25 | + |
| 26 | + // 2. Create Variables |
| 27 | + if (body.variables && body.variables.length > 0) { |
| 28 | + for (var i = 0; i < body.variables.length; i++) { |
| 29 | + var v = body.variables[i]; |
| 30 | + |
| 31 | + var variable = new GlideRecord('item_option_new'); |
| 32 | + variable.initialize(); |
| 33 | + variable.cat_item = catSysId; |
| 34 | + variable.name = v.name.toLowerCase().replace(/ /g, '_'); |
| 35 | + variable.question_text = v.name; |
| 36 | + variable.type = getType(v.type); |
| 37 | + variable.order = (i + 1) * 100; |
| 38 | + var varSysId = variable.insert(); |
| 39 | + |
| 40 | + // Add choices for select box variables |
| 41 | + if (v.choices && v.choices.length > 0) { |
| 42 | + var choices = v.choices.split(','); |
| 43 | + for (var j = 0; j < choices.length; j++) { |
| 44 | + var choice = new GlideRecord('question_choice'); |
| 45 | + choice.initialize(); |
| 46 | + choice.question = varSysId; |
| 47 | + choice.value = choices[j].trim(); |
| 48 | + choice.label = choices[j].trim(); |
| 49 | + choice.insert(); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + result.message = "Catalog item created successfully!"; |
| 56 | + response.setStatus(201); |
| 57 | + |
| 58 | + } catch (e) { |
| 59 | + gs.error("Error creating catalog item: " + e); |
| 60 | + result.message = e.toString(); |
| 61 | + response.setStatus(500); |
| 62 | + } |
| 63 | + |
| 64 | + response.setBody(result); |
| 65 | + |
| 66 | + |
| 67 | + function getCategorySysId(categoryName) { |
| 68 | + var cat = new GlideRecord('sc_category'); |
| 69 | + cat.addQuery('title', categoryName); |
| 70 | + cat.query(); |
| 71 | + if (cat.next()) return cat.sys_id; |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + function getOwner(ownerName) { |
| 76 | + var usr = new GlideRecord('sys_user'); |
| 77 | + usr.addQuery('user_name', ownerName); |
| 78 | + usr.query(); |
| 79 | + if (usr.next()) return usr.sys_id; |
| 80 | + return gs.getUserID(); |
| 81 | + } |
| 82 | + |
| 83 | + function getType(typeName) { |
| 84 | + var map = { |
| 85 | + "single_line_text": 1, |
| 86 | + "multi_line_text": 2, |
| 87 | + "select_box": 3, |
| 88 | + "reference": 8, |
| 89 | + "checkbox": 5 |
| 90 | + }; |
| 91 | + return map[typeName] || 1; |
| 92 | + } |
| 93 | + |
| 94 | +})(request, response); |
| 95 | + |
| 96 | +//Example JSON |
| 97 | +//{ |
| 98 | + "name": "Request New Laptop", |
| 99 | + "category": "Hardware", |
| 100 | + "short_description": "Laptop provisioning form", |
| 101 | + "description": "Allows employees to request a new laptop.", |
| 102 | + "owner": "admin", |
| 103 | + "variables": [ |
| 104 | + { |
| 105 | + "name": "Laptop Model", |
| 106 | + "type": "select_box", |
| 107 | + "choices": "Dell,HP,Lenovo" |
| 108 | + }, |
| 109 | + { |
| 110 | + "name": "RAM Size", |
| 111 | + "type": "select_box", |
| 112 | + "choices": "8GB,16GB,32GB" |
| 113 | + }, |
| 114 | + { |
| 115 | + "name": "Business Justification", |
| 116 | + "type": "multi_line_text" |
| 117 | + } |
| 118 | + ] |
| 119 | +} |
| 120 | + |
0 commit comments