Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.**
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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()) {
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);
Loading