Skip to content

Commit 7b011df

Browse files
authored
Code snippet for syncing integer field on any table (#2595)
* Create sync_integer_field.js * Added additional logic * Create README.md
1 parent 544e747 commit 7b011df

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Scenario: Synchronize integer field that will put records in sequential order.
2+
3+
Example: When you have an integer field on a table and want to be able to add records and have it rearrange order its order sequentially.
4+
5+
Script Logic: An before-update business rule that grabs all the records that currently have an integer field value and puts them into an array. It then adds the new value and puts it into the correct position in the array and updates value so they are in a sequence order.
6+
7+
**You will have to create a custom integer field to be able to use this business rule.**
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Business Rule: 'Before' Update on any table that will have a integer field you want to keep in sync
2+
(function executeRule(current, previous /*null when async*/) {
3+
var tableName = current.getTableName()
4+
5+
//This array will contain the list of records with their current value before rearranging them.
6+
var recordList = [];
7+
8+
var tableRecord = new GlideRecord(tableName);
9+
tableRecord.addNotNullQuery({{insert name of integer field you want to sync}}); //Going to exclude records that don't have a value
10+
tableRecord.addQuery('sys_id', '!=', current.sys_id); //Need to exclude the current record
11+
tableRecord.orderBy({{insert name of integer field you want to sync}});
12+
tableRecord.query();
13+
14+
while(tableRecord.next()) {
15+
recordList.push(tableRecord.sys_id.toString());
16+
}
17+
18+
//Condition check to make sure the record has a value to add to the arry in the correct spot, otherwise no reason to splice the array
19+
if (current.{{insert name of integer field you want to sync}} != '') {
20+
var index = current.{{insert name of integer field you want to sync}} - 1; //Making this one less so it will get added first
21+
recordList.splice(index, 0, current.sys_id.toString()); //This will insert our record into the correct position it needs to be in the list
22+
}
23+
24+
//Reassigning the integer sequentially
25+
for (var i = 0; i < recordList.length; i++) {
26+
if(recordList[i] == current.sys_id.toString()) {
27+
current.{{insert name of integer field you want to sync}} = i + 1;
28+
} else {
29+
var updatedTableRecord = new GlideRecord(tableName);
30+
if (updatedTableRecord.get(recordList[i])) {
31+
updatedTableRecord.{{insert name of integer field you want to sync}} = i + 1;
32+
updatedTableRecord.setWorkflow(false); //Setting the workflow false since we dont want the flow to get triggered since all we are doing is updating the integer field
33+
updatedTableRecord.update()
34+
}
35+
}
36+
37+
38+
})(current, previous);

0 commit comments

Comments
 (0)