Skip to main content

let table = base.getTable("Copy-Orders");

for (let record of resulttxt['Data']) {

    let recordId = await table.createRecordAsync({

        "Order": Number(record['orderNumber']),

        "Due Date": record['dueDate'],

        "Order Total": record['orderTotal'],

        //...

        "Quote Number": [{id: '24229'}],

    });

}

output.text("Created orders records OK!");

 

//tips:The field name is "Quote Number", the associated table name is "Quotes", the associated field is "Quote", and record['quoteNumber'] is the value to be inserted. 

how to deal with the  "Quote Number": [{id: '24229'}], for example,the value of Quote Numbe is  '24229'. thank you.

You’re going to need to get the record ID of the Quotes record and put it in there, and so it’d look like:

Quote Number": [{id: 'recEj1AqeB5coRG1O'}]

To get it I’d suggest using a ‘Find Record’ step and passing that output into the script


You’re going to need to get the record ID of the Quotes record and put it in there, and so it’d look like:

Quote Number": [{id: 'recEj1AqeB5coRG1O'}]

To get it I’d suggest using a ‘Find Record’ step and passing that output into the script

 

could you show me how to use a ‘Find Record’ ,is there a function in script to get it?

thanks a lot.


Ah so sorry, just realized you’re using a Script Extension, not a Run a Script action.  For the Script Extension you’re going to need to write a script to help you get the ID of the record based on the Quote Number value to use

The ‘Find Record’ thing is part of automation actions, and you’d plug in your quote number: and it’d find the record for you: https://support.airtable.com/v1/docs/find-records-automation-action

 

 


Thank you very much. I know how to get the recordId now. but there is an another question, about the function queryQuote.records.find(record => record.Quote == record['quoteNumber']) //?

the field type of the value record['quoteNumber'] is string, and the field type of Quote table

PRIMARY FIELD ‘Quote’ is Number,  the queryQuote.records.find() can not work, how can I deal with this problem? thank you !

 

//1.get orders data

...

let resulttxt=await response.json();

//let resulttxt2=JSON.stringify(resulttxt);

console.log(resulttxt['Data']);

let lengthofdata=resulttxt['Data'].length;

 

let tableQuote = base.getTable("Quotes");

let queryQuote = await tableQuote.selectRecordsAsync();

 

//2.insert orders data

let table = base.getTable("Copy-Orders");

 

for (let record of resulttxt['Data']) {    

    //output.text(Number(record['quoteNumber'])+"-0:");

    let recordToFind = queryQuote.records.find(record => record.Quote == record['quoteNumber']) //?

    output.text((record['quoteNumber'])+"-quoteId:"+ recordToFind.id);

 

    let recordId = await table.createRecordAsync({

        "Order": Number(record['orderNumber']),

        "Quote Number": [{id: recordToFind.id}], //record['quoteNumber']  [{id: 'recnLbZrx0QGtIBYQ'}]

        //"Customer Code":[{id: 'recNG3GuSBM5zXakF'}], 

    });

}

output.text("Created orders records OK!");

 

 


Hm, could you DM me an invite link to your base?  Would make it a lot easier to help!


Hi,

for each Order number, your script will iterate through all Orders table to find a record to link with
sometimes. When both tables about 1000 records each, it doesn’t matter when it takes 200 ms instead of 20ms. But for large tables, like 30k records, running a loop inside other loop means 900 mln operations, that’s too much
.
Instead, you can can run a single loop that creates a Map object - it’s like array of keys and values.
each number will be tied to it’s record ID, so ID will be easy retrievable. 
Note - it works assuming you have column of unique numbers. Map keys are unique by design, so if you have duplicated numbers, only first will be linked, others ignored. It is possible to manage non-unique as well, but a bit more complex.
 

//   after
// let queryQuote = await tableQuote.selectRecordsAsync();
// add this:

let getIDbyNumber=new Map(queryQuote.records.map(r=>
[r.getCellValueAsString('Quote'),r.id]))


getIDbyNumber looks like (example values)
 ["8776", "rect72Rh9Tdm24KKf"]
 ["1235", "rectLGTxsYuz0DsI2"]
 ["1239", "rec70CsmvQooiggic"]
 ["1241", "recB8JZNrjpDEZuJM"]
 ["1249", "recz5bEhgecCYsU0F"]
 ["1250", "recWjGGcHde6LhGCo"]


but since we need array of IDs to link, instead of just id string, we could pack it directly in the same command


TL, DR:
1. add this after   let queryQuote = await tableQuote.selectRecordsAsync() :
 

let getIDbyNumber=new Map(queryQuote.records.map(r=>
[ r.getCellValueAsString('Quote'), [{id:r.id}] ]))

2. Use 
 

"Quote Number": getIDbyNumber.get(record['quoteNumber']) || []

(note: “OR empty array” added to avoid errors if you get number which is absent in table)
also, for debug issues use output.table([...getIDbyNumber])  or console.log(...getIDbyNumber)
because Map not displayed by simple console.log.