How to programmatically create records

I have created two table first_table and second_table.

Now I want to create records programmatically so I imported table, but its still displays errors

TypeError: _ui.table is undefined
	[2]<@https://localhost:9000/__runFrame/bundle.js:83:12
	o@https://localhost:9000/__runFrame/bundle.js:1:265
	o/<@https://localhost:9000/__runFrame/bundle.js:1:316
	runBlock@https://localhost:9000/__runFrame/bundle.js:29:33
	e/<@https://static.airtable.com/js/by_sha/8f4b64fb/block_frame.js:1:2329961
	r@https://static.airtable.com/js/lib/regenerator-runtime.min.js:1:160
	L/<@https://static.airtable.com/js/lib/regenerator-runtime.min.js:4:58
	D/</a[b]@https://static.airtable.com/js/lib/regenerator-runtime.min.js:1:339
	F@https://static.airtable.com/js/by_sha/8f4b64fb/block_frame.js:1:2324534
	a@https://static.airtable.com/js/by_sha/8f4b64fb/block_frame.js:1:2324738

Here is my coding so far

import {initializeBlock, useBase, useRecords, createRecordAsync, table} from '@airtable/blocks/ui';
import React from 'react';
import {base} from '@airtable/blocks';

// create records in myfirst_table
const first_tab = base.getTableByNameIfExists('first_table');
const first_tab1 = table.createRecordAsync([{'lastname': 'Carrots'},{'firstname': 'joy'}, {'email': 'joy@gmail.com'}] );
table.createRecordAsync(first_tab1);


// create records in second_table
const second_tab = base.getTableByNameIfExists('second_table');
const second_tab1 = table.createRecordAsync([{'lastname': 'james'},{'firstname': 'henry'}, {'age': '23'}] );
table.createRecordAsync(second_tab1);

You’re using redundant createRecordAsync()s.

When you do table.createRecordAsync(), table is actually a variable, but you don’t have any variables called table. Meaning, instead of typing table.createRecordAsync() your should be typing first_tab.createRecordAsync(). You don’t need table.createRecordAsync(first_tab1) at all. Follow the same idea for second_tab.

Here’s the Custom Blocks guide to adding/editing data in a table for your reference: https://airtable.com/developers/blocks/guides/write-back-to-airtable

When I run the code below as you said, It alerts record successfully created and display this error in my block

const first_tab = base.getTableByNameIfExists('myfirst_table');
const ok = first_tab.createRecordAsync([{'lastname': 'Carrots'},{'firstname': 'joy'}, {'email': 'joy@gmail.com'}] );


if(ok){
alert('record successfully created and added');
}

here is the error it display in blocks after alerting records successfully created.

Error running block

spawnErrorWithOriginOmittedFromStackTrace@https://localhost:9000/__runFrame/bundle.js:405:13
spawnError@https://localhost:9000/__runFrame/bundle.js:432:10
__getFieldMatching@https://localhost:9000/__runFrame/bundle.js:10143:45
_cellValuesByFieldIdOrNameToCellValuesByFieldId/<@https://localhost:9000/__runFrame/bundle.js:9965:26
_cellValuesByFieldIdOrNameToCellValuesByFieldId@https://localhost:9000/__runFrame/bundle.js:9960:88
createRecordsAsync$/recordsToCreate<@https://localhost:9000/__runFrame/bundle.js:9833:45
createRecordsAsync$@https://localhost:9000/__runFrame/bundle.js:9820:41
tryCatch@https://localhost:9000/__runFrame/bundle.js:74189:40
invoke@https://localhost:9000/__runFrame/bundle.js:74418:30
defineIteratorMethods/</prototype[method]@https://localhost:9000/__runFrame/bundle.js:74241:21
tryCatch@https://localhost:9000/__runFrame/bundle.js:74189:40
invoke@https://localhost:9000/__runFrame/bundle.js:74279:28
callInvokeWithMethodAndArg/<@https://localhost:9000/__runFrame/bundle.js:74314:17
callInvokeWithMethodAndArg@https://localhost:9000/__runFrame/bundle.js:74313:16
enqueue@https://localhost:9000/__runFrame/bundle.js:74336:13
defineIteratorMethods/</prototype[method]@https://localhost:9000/__runFrame/bundle.js:74241:21
[419]</runtime</exports.async@https://localhost:9000/__runFrame/bundle.js:74363:14
createRecordsAsync@https://localhost:9000/__runFrame/bundle.js:9816:35
createRecordAsync$@https://localhost:9000/__runFrame/bundle.js:9641:54
tryCatch@https://localhost:9000/__runFrame/bundle.js:74189:40
invoke@https://localhost:9000/__runFrame/bundle.js:74418:30
defineIteratorMethods/</prototype[method]@https://localhost:9000/__runFrame/bundle.js:74241:21
tryCatch@https://localhost:9000/__runFrame/bundle.js:74189:40
invoke@https://localhost:9000/__runFrame/bundle.js:74279:28
callInvokeWithMethodAndArg/<@https://localhost:9000/__runFrame/bundle.js:74314:17
callInvokeWithMethodAndArg@https://localhost:9000/__runFrame/bundle.js:74313:16
enqueue@https://localhost:9000/__runFrame/bundle.js:74336:13
defineIteratorMethods/</prototype[method]@https://localhost:9000/__runFrame/bundle.js:74241:21
[419]</runtime</exports.async@https://localhost:9000/__runFrame/bundle.js:74363:14
createRecordAsync@https://localhost:9000/__runFrame/bundle.js:9635:35
[2]<@https://localhost:9000/__runFrame/bundle.js:62:20
o@https://localhost:9000/__runFrame/bundle.js:1:265
o/<@https://localhost:9000/__runFrame/bundle.js:1:316
runBlock@https://localhost:9000/__runFrame/bundle.js:29:33
e/<@https://static.airtable.com/js/by_sha/8f4b64fb/block_frame.js:1:2329961
r@https://static.airtable.com/js/lib/regenerator-runtime.min.js:1:160
L/<@https://static.airtable.com/js/lib/regenerator-runtime.min.js:4:58
D/</a[b]@https://static.airtable.com/js/lib/regenerator-runtime.min.js:1:339
F@https://static.airtable.com/js/by_sha/8f4b64fb/block_frame.js:1:2324534
a@https://static.airtable.com/js/by_sha/8f4b64fb/block_frame.js:1:2324738

Try this:

const ok = first_tab.createRecordAsync({'lastname': 'Carrots', 'firstname': 'joy', 'email': 'joy@gmail.com'});

The difference is no straight brackets ([ ]) and all fields are within one set of curly brackets ({ })

Thanks. it works perfectly

1 Like

Please Sir, can you also help me out with this issue at How to add files along with other records

Thanks

Sorry, I haven’t experimented with uploading files through a custom block so I won’t be able to help you with that request.

ok. between thanks alot

@Kamille_Parks please can you help me out on how to delete and update records. below is the 2 seperate links to the issue. Thanks

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.