Filter records before being copied

Hi everyone,

I have no scripting knowledge and I have the below script from our community

SCRIPT
let table = base.getTable(“CopyRows”);
let query = await table.selectRecordsAsync();
let recordsArray = ;
for (let record of query.records) {
let quantity = record.getCellValue(“Quantity”);
if (quantity > 1) {
for (let i = 0; i < quantity-1; i++) {
recordsArray.push({
fields: {
“Name”: record.getCellValue(“Name”),
“Status”: record.getCellValue(“Status”),
“Quantity2”: record.getCellValue(“Quantity2”),
“Quantity”: quantity
}
})
}
}
let quantity2 = record.getCellValue(“Quantity2”);
if (quantity2 > 1) {
for (let i = 0; i < quantity2-1; i++) {
recordsArray.push({
fields: {
“Name”: record.getCellValue(“Name”),
“Status”: record.getCellValue(“Status”),
“Quantity”: record.getCellValue(“Quantity”),
“Quantity2”: 1
}
})
}
}
}
console.log(recordsArray);
let YOUR_TABLE = base.getTable(“YOUR_TABLE”);
let newRecords = await YOUR_TABLE.createRecordsAsync(recordsArray);

The script working fine for me except I would like to filter only records which contain certain values to be copied over to the other table.
For example: I have a columns name ‘Load Out No.’ with the values such as Load Out 1, Load Out 2, Load Out 3 etc.
I would love the script to prompt for input where I can enter ‘2’ as the value and only records with Load Out 2 being copied over.

Thanks in advance for all your help.

Welcome to the community, @Master_Poultry! :smiley: You mention a couple times that you’re copying data to another table, but you haven’t described your table setup. Knowing more about the design of your base will help us know what kind of advice to give. The script alone doesn’t contain enough detail.

On a side note, when pasting a script into a post, it’s preferable to mark it as preformatted text. In your post, you marked it as a blockquote, which doesn’t preserve the indentation, doesn’t show any syntax highlighting like the preformatted text option does, and leaves all quotation marks (single and double) as styled quotes vs straight quotes. The easiest way to delineate a large block of preformatted text is to surround it on both ends with graves triplets (the grave is the inverted apostrophe symbol that shares the key with the tilde, and looks like this: ` ). While editing, you’ll see something like this:

```
let table = base.getTable(“CopyRows”);
let query = await table.selectRecordsAsync();
let recordsArray = ;
// Etc.
```

…which will look like this when formatted:

let table = base.getTable("CopyRows");
let query = await table.selectRecordsAsync();
let recordsArray = [];
// Etc.
1 Like