# Records: CRUD Operations

> Part of the NocoDB documentation (Workflows > Scripts > Examples > Generic). 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/demo/records-CRUD
Last updated: 2026-10-03

A NocoDB script that creates, reads, updates and deletes records in a table.

This script shows how to do basic CRUD (Create, Read, Update, Delete) operations on records in a NocoDB table. The script does these steps:

1. It creates a table if the table does not exist.
2. It adds fields.
3. It creates demo records.
4. It updates those records.
5. It deletes the records.

For more information about the NocoDB API, refer to the [NocoDB data API documentation](https://nocodb.com/apis/v3/data).

```javascript
const tableName = 'Contacts';
const fields = [
  { name: 'Name', type: 'SingleLineText' },
  { name: 'Email', type: 'Email' },
  {
    name: 'Status',
    type: 'SingleSelect',
    options: {
      choices: [
        { title: 'Active' },
        { title: 'Pending' },
        { title: 'Inactive' }
      ]
    }
  }
];

// Step 1: Create table if it doesn’t exist
let table = base.getTable(tableName);
if (!table) {
  table = await base.createTableAsync(tableName, []);
}

// Step 2: Create any missing fields
const existingFieldNames = table.fields.map(f => f.name);
for (let field of fields) {
  if (!existingFieldNames.includes(field.name)) {
    await table.createFieldAsync({ name: field.name, type: field.type, options: field.options });
  }
}

// Step 3: Create demo records
const recordsToCreate = [
  { fields: {
    'Name': 'John Doe',
    'Email': 'john.doe@example.com',
    'Status': 'Active'
  }},
  { fields: {
    'Name': 'Jane Smith',
    'Email': 'jane.smith@example.com',
    'Status': 'Pending'
  }},
  { fields: {
    'Name': 'Bob Johnson',
    'Email': 'bob.johnson@example.com',
    'Status': 'Active'
  }}
];

const createdRecords = await table.createRecordsAsync(recordsToCreate);
output.text(`✅ Created ${createdRecords.length} new records.`);

// Step 4: Read the created records
const records = await table.selectRecordsAsync();
const created = records.records.filter(r => createdRecords.includes(r.id));

const displayBeforeUpdate = created.map(r => ({
  Name: r.getCellValue('Name'),
  Email: r.getCellValue('Email'),
  Status: r.getCellValue('Status')
}));

output.text('📖 Records after creation:');
output.table(displayBeforeUpdate);

// Step 5: Update status of each record to "Inactive"
const updates = created.map(r => ({
  id: r.id,
  fields: {
    Status: 'Inactive'
  }
}));

await table.updateRecordsAsync(updates);

const updated = await table.selectRecordsAsync();
const updatedDisplay = updated.records.filter(r => createdRecords.includes(r.id)).map(r => ({
  Name: r.getCellValue('Name'),
  Email: r.getCellValue('Email'),
  Status: r.getCellValue('Status')
}));

output.text('🔁 Records after status update:');
output.table(updatedDisplay);

// Step 6: Delete the created records
await table.deleteRecordsAsync(createdRecords);
output.text(`🗑️ Deleted ${createdRecords.length} records.`);

```

---

## Related pages

- [Table Operations](https://nocodb.com/docs/workflows/scripts/examples/demo/table-operations.md): A NocoDB script that checks if a table exists, creates a table and lists all tables in the base.
- [Fields: Text-Based](https://nocodb.com/docs/workflows/scripts/examples/demo/field-text-based.md): A NocoDB script that creates and manages text-based fields.
- [Fields: Numerical](https://nocodb.com/docs/workflows/scripts/examples/demo/field-number-based.md): A NocoDB script that creates numerical fields for quantities, percentages, currency and more.
- [Fields: Date & Time](https://nocodb.com/docs/workflows/scripts/examples/demo/field-date-time.md): A NocoDB script that creates Date, Time, DateTime and Duration fields.
- [Fields: Select](https://nocodb.com/docs/workflows/scripts/examples/demo/field-select-based.md): A NocoDB script that creates single select and multi select fields to categorize data.
- [Fields: System](https://nocodb.com/docs/workflows/scripts/examples/demo/field-system.md): A NocoDB script that creates system fields to capture audit information automatically.
- [Fields: User & Collaborator](https://nocodb.com/docs/workflows/scripts/examples/demo/field-user.md): A NocoDB script that creates a User field to assign or reference users in records.
- [Fields: Virtual & Formula](https://nocodb.com/docs/workflows/scripts/examples/demo/field-virtual.md): A NocoDB script that adds calculated or derived data with virtual fields.
