Hello beautiful people.
I have a table with 100 kids channels maybe more in future. as we have different territories we push our channels to, we needed to shuffle those channels according to the territory. so manual adjust is needed.
The main request here is : to be able to change a channel order Manually like I wanted Tigilli channel to be No 1 instead of 5. Now I have Smurfs TV as 1 , it should update to no 2 and Disney Plus channel is there is No 2 , it should update to No 3 and goes on till the end of list.
As Logic : when I change the order value in a #Order field what should happens is all other higher values including the same older value that i have changed should update (+1) the numbers order sequentially according to that to still give me 100 channels with the new order.
I have tried this script, but I'm not going anywhere. Can you please help me rewrite the script in a way that works
const tableName = "Table 1"; // my table name
const fieldName = "Order"; // My order field name
// Fetch all records in the table
let table = base.getTable(tableName);
let records = await table.selectRecordsAsync();
// Get the modified record
let modifiedRecord = input.record;
if (modifiedRecord) {
let modifiedValue = modifiedRecord.getCellValue(fieldName);
// Create an array to hold the updated order values
let updatedOrderValues = [];
// Iterate through all records
for (let record of records.records) {
let currentValue = record.getCellValue(fieldName);
if (currentValue <= modifiedValue) {
// If the current value is less than or equal to the modified value, keep it as is
updatedOrderValues.push(currentValue);
} else {
// If the current value is greater than the modified value, increment it by 1
updatedOrderValues.push(currentValue + 1);
}
}
// Update the order values for all records
for (let i = 0; i < records.records.length; i++) {
let record = records.records[i];
await table.updateRecordAsync(record, {
[fieldName]: updatedOrderValues[i]
});
}
}
Can you please help modifying the script or send me a base that is working with the same logic ?
My Best
Ahmed