Skip to content

Commit f932b62

Browse files
authored
Generate next unique value based on sequence (ServiceNowDevProgram#740)
* feat: next-unique-sequence action to generate next unique id based on sequence a functionality using action to generate next unique id based on sequence available in table column. * doc: next-unique-sequence documentation for using functionality documentation includes the working of the main function inside the action and also about input parameters.
1 parent 863a804 commit f932b62

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Generate Unique Value Based on Sequence
2+
3+
The `execute()` function in action is generates a unique sequence value based on the existing sequence in a column of a table. This function inside a action script is designed to be used in database environments. It takes the following input parameters:
4+
5+
- `table`: The name of the table to generate the unique value for.
6+
- `column`: The name of the column to generate the unique value for.
7+
- `default_value`: The default value to use if there are no records in the table.
8+
- `useLowerUpperBoth`: A Boolean value indicating whether to use both lower and upper case characters when generating the next sequence value.
9+
10+
## Function Behavior
11+
12+
1. The function begins by querying the specified table to retrieve all the values in the specified column.
13+
14+
2. If there are records in the table:
15+
- The function then generates the next sequence value based on the last value in the column.
16+
- It automatically detects the type of sequence value to generate based on the postfix of the last value in the column:
17+
- If the postfix ends with digits, the function generates a numeric sequence value.
18+
- If the postfix ends with alphabetic characters, the function generates an alphabetic sequence value.
19+
- If the `useLowerUpperBoth` parameter is set to `true`, the function will use both lower and upper case characters when generating alphabetic sequence values.
20+
- If the `useLowerUpperBoth` parameter is set to `false`, the function will only use upper case characters when generating alphabetic sequence values.
21+
22+
3. If there are no records in the table, the function returns the `default_value`.
23+
24+
4. The function handles any exceptions by setting the output value to `null`.

0 commit comments

Comments
 (0)