From 5e1e2325257f8238daf9a33f33f5b24bd6fb3018 Mon Sep 17 00:00:00 2001 From: DavidMarcial <60865187+DavidMarcial@users.noreply.github.com> Date: Tue, 28 Oct 2025 14:51:16 +0100 Subject: [PATCH 1/3] Create sync_integer_field.js --- .../Sync Integer Field/sync_integer_field.js | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Server-Side Components/Business Rules/Sync Integer Field/sync_integer_field.js diff --git a/Server-Side Components/Business Rules/Sync Integer Field/sync_integer_field.js b/Server-Side Components/Business Rules/Sync Integer Field/sync_integer_field.js new file mode 100644 index 0000000000..2bbeb23ecf --- /dev/null +++ b/Server-Side Components/Business Rules/Sync Integer Field/sync_integer_field.js @@ -0,0 +1,22 @@ +// Business Rule: 'Before' Update on any table that will have a integer field you want to keep in sync +(function executeRule(current, previous /*null when async*/) { + var tableName = current.getTableName() + + //This array will contain the list of records with their current value before rearranging them. + var recordList = []; + + var tableRecord = new GlideRecord(tableName); + tableRecord.addNotNullQuery({{insert name of integer field you want to sync}}); //Going to exclude records that don't have a value + tableRecord.addQuery('sys_id', '!=', current.sys_id); //Need to exclude the current record + tableRecord.orderBy({{insert name of integer field you want to sync}}); + tableRecord.query(); + + while(tableRecord.next()) { + projectList.push(tableRecord.sys_id.toString()); + } + + + + + +})(current, previous); From 06eaf0f698ca97aee1bf91db5b5c946e42752e1e Mon Sep 17 00:00:00 2001 From: DavidMarcial <60865187+DavidMarcial@users.noreply.github.com> Date: Tue, 28 Oct 2025 16:09:24 +0100 Subject: [PATCH 2/3] Added additional logic --- .../Sync Integer Field/sync_integer_field.js | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Server-Side Components/Business Rules/Sync Integer Field/sync_integer_field.js b/Server-Side Components/Business Rules/Sync Integer Field/sync_integer_field.js index 2bbeb23ecf..787ef4241f 100644 --- a/Server-Side Components/Business Rules/Sync Integer Field/sync_integer_field.js +++ b/Server-Side Components/Business Rules/Sync Integer Field/sync_integer_field.js @@ -12,11 +12,27 @@ tableRecord.query(); while(tableRecord.next()) { - projectList.push(tableRecord.sys_id.toString()); + recordList.push(tableRecord.sys_id.toString()); } + //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 + if (current.{{insert name of integer field you want to sync}} != '') { + var index = current.{{insert name of integer field you want to sync}} - 1; //Making this one less so it will get added first + recordList.splice(index, 0, current.sys_id.toString()); //This will insert our record into the correct position it needs to be in the list + } - + //Reassigning the integer sequentially + for (var i = 0; i < recordList.length; i++) { + if(recordList[i] == current.sys_id.toString()) { + current.{{insert name of integer field you want to sync}} = i + 1; + } else { + var updatedTableRecord = new GlideRecord(tableName); + if (updatedTableRecord.get(recordList[i])) { + updatedTableRecord.{{insert name of integer field you want to sync}} = i + 1; + 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 + updatedTableRecord.update() + } + } })(current, previous); From 14963155710f0b29bdbe4f67b458b15ec9dc694c Mon Sep 17 00:00:00 2001 From: DavidMarcial <60865187+DavidMarcial@users.noreply.github.com> Date: Tue, 28 Oct 2025 11:15:47 -0700 Subject: [PATCH 3/3] Create README.md --- .../Business Rules/Sync Integer Field/README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Server-Side Components/Business Rules/Sync Integer Field/README.md diff --git a/Server-Side Components/Business Rules/Sync Integer Field/README.md b/Server-Side Components/Business Rules/Sync Integer Field/README.md new file mode 100644 index 0000000000..f464427639 --- /dev/null +++ b/Server-Side Components/Business Rules/Sync Integer Field/README.md @@ -0,0 +1,7 @@ +Scenario: Synchronize integer field that will put records in sequential order. + +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. + +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. + +**You will have to create a custom integer field to be able to use this business rule.**