Help

Re: Airtable example script for "Find and Replace"

1684 3
cancel
Showing results for 
Search instead for 
Did you mean: 
Chris_Smith3
4 - Data Explorer
4 - Data Explorer

I wish to find an integer between 1 & 5 in a number field and replace it with a zero (0).
The example script gives an error message:
“TypeError: originalValue.replace is not a function
at main on line 20”
I have never programmed a Java script!
Please help.

5 Replies 5

Welcome to the community, @Chris_Smith3! :grinning_face_with_big_eyes: What is the script you’re using? When pasting the code here, please wrap it in graves triplets, like this:

```
Code in here
More code
Beautiful code
```

That displays it as preformatted text, which keeps it “clean” and free of things like styled quotes:

Code in here
More code
Beautiful code

G’day Justin,
Thank you for your response.

I am using the example script. The values for getTable and getField were automatically added and are suitable.
Line 20 is shown in/italics/:

let table = base.getTable(“Shopping”);
let field = table.getField(“Buy”);
let findText =await input.textAsync(‘Enter text to find:’);
let replaceText =await input.textAsync(‘Enter to replace matches with:’);
// Load all of the records in the table
let result =await table.selectRecordsAsync();
// Find every record we need to update
let replacements =;
for(let record of result.records){
let originalValue = record.getCellValue(field);
// Skip records which don’t have the value set, so the value is null
if(!originalValue){
continue;
}
/let// newValue
/
/=// originalValue//.//replace//(//findText//,// replaceText//);/

if(originalValue !== newValue){
replacements.push({
record,
before: originalValue,
after: newValue,
});
}
}
if(!replacements.length){
output.text(‘No replacements found’);
}else{
output.markdown(’## Replacements’);
output.table(replacements);
let shouldReplace
=await input.buttonsAsync(‘Are you sure you want to save these changes?’,[
{label:‘Save’, variant:‘danger’},
{label:‘Cancel’},
]);
if(shouldReplace ===‘Save’){
// Update the records
let updates = replacements.map(replacement =>({
id: replacement.record.id,
fields:{
[field.id]: replacement.after,
}
}));
// Only up to 50 updates are allowed at one time, so do it in batches
while(updates.length >0){
await table.updateRecordsAsync(updates.slice(0,50));
updates = updates.slice(50);
}
}
}

See you some day,

Chris Smith

Thanks for that. I probably should have been more clear about the graves triplets to surround the code. The grave (`) is the backwards-apostrophe character that shares a key (on most keyboards) with the tilde (~). Sorry for the confusion.

That aside, your code does clarify the situation. Your script is collecting data from the {Buy} field into the originalValue variable. After confirming that you’ve actually got data in that variable, you’re trying to use the replace() method on that value. The replace() method only works on strings, though, so my guess is that the {Buy} field isn’t returning a string. What type of field is it?

Even if the {Buy} field doesn’t natively return a string, you can force its data to become a string by using record.getCellValueAsString instead of record.getCellValue. That should allow the replace() method to run as expected.

Except that if the field is not a text field, trying to write a string back to the field probably won’t work.

In the original post, @Chris_Smith3 said that he is looking for an integer in a number field. Thus the logic of using the replace function also won’t work. He needs to check if the number is between 1 and 5.

if ((originalValue > 1) && (originalValue < 5)) {
  replacements.push({
    record,
    before: originalValue,
    after: 0,
  })
}

Oops! I forgot about that. I was too focused on the code, and forgot to check the original details.