# Save View Ordering to Field

> Part of the NocoDB documentation (Workflows > Scripts > Examples). Index of all pages: https://nocodb.com/llms.txt. Any docs page is available as Markdown by adding `.md` to its URL.

URL: https://nocodb.com/docs/workflows/scripts/examples/save-view-ordering
Last updated: 2026-10-03

A NocoDB script that saves the record order of a view as sequential numbers in a numeric 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.

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

1. **Select Source View**: Select the view that has the record order you want.
2. **Choose Numeric Field**: Select a number field to store the sequence values.
3. **Capture Ordering**: The script reads the current record order from the view.
4. **Assign Numbers**: The script writes sequential numbers (1, 2, 3...) to the selected field.
5. **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 documentation
```

**Step 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: 4
```

**Step 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.

---

## Related pages

- [API Reference Manual Examples](https://nocodb.com/docs/workflows/scripts/examples/api-ref-manual-examples.md): An index of the NocoDB Scripts API reference examples, grouped by category, for common scripting tasks.
- [Find and Replace](https://nocodb.com/docs/workflows/scripts/examples/find-and-replace.md): A NocoDB script that finds and replaces text in any text-based field across records, with a preview and a confirmation.
- [Convert Attachments to URLs](https://nocodb.com/docs/workflows/scripts/examples/convert-attachments-to-urls.md): A NocoDB script that converts attachment fields into comma-separated lists of URLs for external services.
- [Randomize Values](https://nocodb.com/docs/workflows/scripts/examples/randomize-values.md): A NocoDB script that fills empty fields with random data, for many field types, with ranges and constraints that you set.
- [Validate Emails](https://nocodb.com/docs/workflows/scripts/examples/validate-emails.md): A NocoDB script that finds and lists all invalid email addresses in email fields to check data quality.
- [Get Select Options](https://nocodb.com/docs/workflows/scripts/examples/select-options.md): A NocoDB script that lists all options of a Single Select or Multiple Select field in one of three formats.
- [Link Records by Field](https://nocodb.com/docs/workflows/scripts/examples/link-records-by-field.md): A NocoDB script that links records in two tables by matching field values, with pagination and a summary report.
- [Unique Field Values Summary](https://nocodb.com/docs/workflows/scripts/examples/unique-field-values-summary.md): A NocoDB script that shows the unique values in a field and how often each occurs, as a summary and a table.
