|
| 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