|
| 1 | +var GetChoicesFromDT = Class.create(); |
| 2 | +GetChoicesFromDT.prototype = Object.extendsObject(global.AbstractAjaxProcessor, { |
| 3 | + |
| 4 | + getChoices: function() { |
| 5 | + |
| 6 | + /** |
| 7 | + * Gets the defined choices for the passed in catalog item |
| 8 | + * |
| 9 | + * @author Laszlo Balla |
| 10 | + * @param {String} sysparm_cat_item |
| 11 | + * The sys_id of the catalog item to get choices for - mandatory |
| 12 | + * @param {String} sysparm_cat_variable |
| 13 | + * Value from an additional catalog variable to evaluate as part of your decision - optional |
| 14 | + * @return {String} |
| 15 | + * A stringified array (since it goes to client script) of choices |
| 16 | + */ |
| 17 | + |
| 18 | + |
| 19 | + /** |
| 20 | + * In addition to the above, the following variable MUST be set for the script to work: |
| 21 | + * |
| 22 | + ** decisionTableId : Sys ID of the decision table. Store in a system property and set with gs.getProperty() |
| 23 | + ** dtInput1, 2, etc. : the technical names of the Decision Table inputs |
| 24 | + ** resultColumn : the technical name of the result column of your Decision Table that has the choices set |
| 25 | + */ |
| 26 | + |
| 27 | + var catItem = gs.nil(this.getParameter('sysparm_cat_item')) ? null : this.getParameter('sysparm_cat_item'); // Mandatory parameter |
| 28 | + var catVar = gs.nil(this.getParameter('sysparm_cat_variable')) ? null : this.getParameter('sysparm_cat_variable'); // Optional parameter example (variable from record producer). Multiple as needed, or remove if not. |
| 29 | + var decisionTableId = ''; //Sys ID of the decision table. Store in a system property and set with gs.getProperty() |
| 30 | + var dtInput1 = 'u_catalog_item'; // Make sure you set this to the technical name of the first input of your Decision Table |
| 31 | + var dtInput2 = 'u_catalog_variable'; // Make sure you set this to the technical name of the second input of your Decision Table, if you have one. Multiply as needed, or remove if not. |
| 32 | + var resultColumn = 'u_choice_result'; // Set this to the technical name of the result column that contains your choices |
| 33 | + var answerArray = []; |
| 34 | + var choiceArr = []; |
| 35 | + var iter1 = 0; |
| 36 | + |
| 37 | + if (!gs.nil(catItem) && !gs.nil(decisionTableId)) { |
| 38 | + var choiceQuery = 'var__m_sys_decision_multi_result_element_' + decisionTableId; |
| 39 | + var decisonTable = new sn_dt.DecisionTableAPI(); |
| 40 | + var inputs = new Object(); |
| 41 | + inputs[dtInput1] = catItem; |
| 42 | + |
| 43 | + // Repeat this block as necessary with additional parameters and inputs |
| 44 | + if (!gs.nil(catVar)) { |
| 45 | + inputs[dtInput2] = catVar; |
| 46 | + } |
| 47 | + |
| 48 | + var dtResponse = decisonTable.getDecisions(decisionTableId, inputs); |
| 49 | + while (iter1 < dtResponse.length) { |
| 50 | + answerArray.push(dtResponse[iter1]['result_elements'][resultColumn].toString()); |
| 51 | + iter1++; |
| 52 | + } |
| 53 | + // Now find the the actual choices with labels |
| 54 | + var choiceGr = new GlideRecord('sys_choice'); |
| 55 | + choiceGr.addQuery('name', choiceQuery); |
| 56 | + choiceGr.addQuery('value', 'IN', answerArray.toString()); |
| 57 | + choiceGr.setLimit(30); // The Choice table is huge, so I recommend setting a reasonable query limit. You should have an idea of the max # of results anyway. |
| 58 | + choiceGr.query(); |
| 59 | + while (choiceGr.next()) { |
| 60 | + var choice = {}; |
| 61 | + choice['value'] = choiceGr.getValue('value'); |
| 62 | + choice['label'] = choiceGr.getValue('label'); |
| 63 | + choiceArr.push(choice); |
| 64 | + } |
| 65 | + |
| 66 | + return JSON.stringify(choiceArr); // Return a stringified array to the client |
| 67 | + |
| 68 | + } else { |
| 69 | + gs.error('GetChoicesFromDT Script include did not run as the catItem mandatory variable is null: ' + catItem + ' or decision table sys_id is empty: ' + decisionTableId); |
| 70 | + return; |
| 71 | + } |
| 72 | + }, |
| 73 | + |
| 74 | + type: 'GetChoicesFromDT' |
| 75 | +}); |
0 commit comments