Save View Ordering to Field
This script reads the current order of records in a selected view. It saves the order as sequential numbers (1, 2, 3...) in a numeric field that you select. Use it to keep a manual record order and to apply the same order to other views.
const config = input.config({
title: 'Save view ordering to field',
description: `A script to save the record ordering of a particular view to a specified field in the table.
This ordering can then be applied to other views by sorting on that field.
Useful when you want to apply the order from one view to another view after manually reordering records.`,
items: [
input.config.table('sourceTable', {
label: 'Table',
description: 'The table containing the view you want to save the ordering from'
}),
input.config.view('sourceView', {
label: 'Source View',
description: 'The view whose ordering you want to save to a field',
parentTable: 'sourceTable'
}),
input.config.field('orderingField', {
label: 'Order field',
description: 'The field you want to save the ordering to, this must be a number field.',
parentTable: 'sourceTable'
})
]
});
if (config.orderingField.type !== UITypes.Number) {
output.markdown(`**Error**: Order field must be of type number.`);
return;
}
const { sourceTable, sourceView, orderingField } = config;
output.markdown(`#### Assigning ordering from view "${sourceView.name}" to field "${orderingField.name}"`);
// Process records in chunks to avoid loading everything into memory
const queryResult = await sourceView.selectRecordsAsync();
const UPDATE_BATCH_SIZE = 10;
let orderNumber = 1;
let totalProcessed = 0;
while (true) {
// Get only the new records from this chunk (not already processed)
const newRecords = queryResult.records.slice(totalProcessed);
// If no new records to process, we're done
if (newRecords.length === 0) {
break;
}
output.markdown(`Processing chunk of ${newRecords.length} new records (starting from order ${orderNumber})`);
// Prepare updates for current chunk
const recordUpdates = [];
for (const record of newRecords) {
recordUpdates.push({
id: record.id,
fields: { [orderingField.name]: orderNumber }
});
orderNumber++;
}
// Update records in batches of 10 within this chunk
let chunkProcessed = 0;
while (chunkProcessed < recordUpdates.length) {
const batchStart = chunkProcessed;
const batchEnd = Math.min(chunkProcessed + UPDATE_BATCH_SIZE, recordUpdates.length);
const currentBatch = recordUpdates.slice(batchStart, batchEnd);
output.markdown(` → Updating records ${totalProcessed + batchStart + 1} to ${totalProcessed + batchEnd}`);
await sourceTable.updateRecordsAsync(currentBatch);
chunkProcessed = batchEnd;
}
totalProcessed += recordUpdates.length;
// Try to load more records if available
if (queryResult.hasMoreRecords) {
await queryResult.loadMoreRecords();
}
}
output.markdown(`**Done!** Successfully assigned ordering numbers to ${totalProcessed} records.`);Use Cases
- Preserve Manual Ordering: Save custom record orders that took time to organize.
- Cross-View Consistency: Apply the same order to many views.
- Priority Systems: Create numbered priority sequences for tasks or items.
- Reference Ordering: Keep a standard record order for reports.
- Backup Arrangements: Save the order before you apply filters or sorts.
How it Works
- Select Source View: Select the view that has the record order you want.
- Choose Numeric Field: Select a number field to store the sequence values.
- Capture Ordering: The script reads the current record order from the view.
- Assign Numbers: The script writes sequential numbers (1, 2, 3...) to the selected field.
- Apply Elsewhere: Sort other views by this numeric field to get the same order.
Requirements
- A Number field in your table (an integer field is best)
- A view that already has the record order you want
- Write permissions to change the selected numeric field
Workflow Example
Step 1: Arrange Records
View: "Priority Tasks"
Current Order:
- Complete project proposal
- Review budget requirements
- Schedule team meeting
- Update documentationStep 2: Run Script
- Select the "Priority Tasks" view.
- Select the "Sort Order" number field.
- Run the script.
Step 3: Result
Records with new Sort Order values:
- Complete project proposal → Sort Order: 1
- Review budget requirements → Sort Order: 2
- Schedule team meeting → Sort Order: 3
- Update documentation → Sort Order: 4Step 4: Reproduce Ordering
- In any other view, sort by the "Sort Order" field.
- The records show in the same sequence.
Benefits
- Persistent Ordering: Manual record orders stay after view changes and filters.
- Reusable Sequences: Apply the same order to many views.
- Reference Standard: Create a standard order for your data.
- Time Saving: You do not have to arrange records manually in each view.
- Flexible Application: Sort by the numeric field when you need the saved order.
Best Practices
- Use an integer Number field for clean sequential values.
- Name the field clearly (for example, "Priority Order", "Display Sequence" or "Sort Index").
- Document the source view so that team members understand the order.
- Update periodically when the order you want changes.
- Combine with filters to create ordered subsets of your data.
Last updated on
Latest product updates?See Changelog