|
| 1 | +(function execute(inputs, outputs) { |
| 2 | + // ... code ... |
| 3 | + try { |
| 4 | + var records = []; // Initialize an array to store records. |
| 5 | + var tableGR = new GlideRecord(inputs.table); // Create a GlideRecord for the specified table. |
| 6 | + tableGR.query(); // Execute the query to retrieve records. |
| 7 | + while (tableGR.next()) { |
| 8 | + if (tableGR.hasOwnProperty(inputs.column)) { |
| 9 | + // Check if the record has the specified column. |
| 10 | + records.push(tableGR[inputs.column].getDisplayValue()); // Add the column's display value to the records array. |
| 11 | + } |
| 12 | + } |
| 13 | + |
| 14 | + if (records.length > 0) { |
| 15 | + // If there are records in the array. |
| 16 | + outputs.value = records[records.length - 1]; // Set the output to the last record's value. |
| 17 | + |
| 18 | + while (true) { |
| 19 | + outputs.value = nextSequence(outputs.value, inputs.useLowerUpperBoth); // Generate the next sequence value. |
| 20 | + if (!records.includes(outputs.value)) { |
| 21 | + // Check if the generated value is not in the records array. |
| 22 | + break; // Exit the loop. |
| 23 | + } |
| 24 | + } |
| 25 | + } else { |
| 26 | + // If there are no records. |
| 27 | + outputs.value = inputs.default_value; // Set the output to the default value. |
| 28 | + } |
| 29 | + } catch (e) { |
| 30 | + outputs.value = null; // Handle any exceptions by setting the output to null. |
| 31 | + } |
| 32 | + |
| 33 | +})(inputs, outputs); |
| 34 | + |
| 35 | +// Function to generate the next sequence based on the input value and options. |
| 36 | +function nextSequence(input, useLowerUpperBoth) { |
| 37 | + if (/\d+$/.test(input.toString())) { |
| 38 | + // If the input ends with digits. |
| 39 | + var postfix = input.match(/\d+$/)[0]; // Extract the digits. |
| 40 | + var initLength = postfix.length; |
| 41 | + postfix = parseInt(postfix); |
| 42 | + ++postfix; |
| 43 | + if (postfix.toString().length < initLength) { |
| 44 | + postfix = padStart(postfix.toString(), initLength, '0'); // Ensure consistent length by padding with zeros. |
| 45 | + } |
| 46 | + return input.replace(/\d+$/, postfix); // Replace the digits with the incremented value. |
| 47 | + } else { |
| 48 | + // If the input ends with alphabetic characters. |
| 49 | + var postfix = input.match(/[A-Za-z]+$/)[0]; // Extract the alphabetic characters. |
| 50 | + var index = postfix.length - 1; |
| 51 | + while (true) { |
| 52 | + if (index === -1) { |
| 53 | + // If all characters have been processed. |
| 54 | + var charCode = postfix.charCodeAt(0); |
| 55 | + if (useLowerUpperBoth === true) { |
| 56 | + postfix = 'A' + postfix; |
| 57 | + } else { |
| 58 | + postfix = (charCode < 96 ? 'A' : 'a') + postfix; |
| 59 | + } |
| 60 | + break; |
| 61 | + } |
| 62 | + var charCode = postfix.charCodeAt(index); |
| 63 | + |
| 64 | + if (useLowerUpperBoth) { |
| 65 | + // If both lower and upper case characters are allowed. |
| 66 | + if (charCode == 90) { |
| 67 | + postfix = replaceAt(postfix, index, String.fromCharCode(97)); |
| 68 | + break; |
| 69 | + } else if (charCode == 122) { |
| 70 | + postfix = replaceAt(postfix, index, String.fromCharCode(97)); |
| 71 | + index--; |
| 72 | + } else { |
| 73 | + postfix = replaceAt(postfix, index, String.fromCharCode(charCode + 1)); |
| 74 | + break; |
| 75 | + } |
| 76 | + } else { |
| 77 | + // If only upper case characters are allowed. |
| 78 | + if (charCode == 90) { |
| 79 | + postfix = replaceAt(postfix, index, String.fromCharCode(65)); |
| 80 | + index--; |
| 81 | + } else if (charCode == 122) { |
| 82 | + postfix = replaceAt(postfix, index, String.fromCharCode(97)); |
| 83 | + index--; |
| 84 | + } else { |
| 85 | + postfix = replaceAt(postfix, index, String.fromCharCode(charCode + 1)); |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return input.replace(/[A-Za-z]+$/, postfix); // Replace the alphabetic characters with the updated value. |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// Function to replace a character at a specific index in a string. |
| 96 | +function replaceAt(str, index, ch) { |
| 97 | + return str.substring(0, index) + ch + str.substring(index + 1); |
| 98 | +} |
| 99 | + |
| 100 | +// Function to pad a string with a specified character to a certain length. |
| 101 | +function padStart(str, length, init) { |
| 102 | + return str.length < length ? padStart(init + str, length, init) : str; |
| 103 | +} |
0 commit comments