Help

Re: Help me fix a bug within an if then statement

473 2
cancel
Showing results for 
Search instead for 
Did you mean: 
Ariel_Dagan
4 - Data Explorer
4 - Data Explorer

I created a table that has cells with email templates I want to send. The email templates in specific cells have tags that reference specific records I have chosen. I am trying to pass the email templates to an If Then statement in a script but the output it puts is with the tags instead of the record information.

Below you can see the script, followed by the output, followed by the output I want. Do you have any idea how to fix this?

SCRIPT
// Change this name to use a different table
let deals = base.getTable(“Deals”);
let templates = base.getTable(“Templates”);

// Prompt the user to pick a record from deals then from templates
// If this script is run from a button field, this will use the button’s record instead.
output.text(“Templates matched with Records”)
let record = await input.recordAsync(‘Select a record to use’, deals);
let temp = await input.recordAsync(‘Select a template to use’, templates);

// You can use temp.getCellValue("Field name") to access the template

var selectedTemp = temp.getCellValueAsString(“written template”);
console.log(selectedTemp)
// Customize this section to handle the selected record
if (record) {
// cell values from the template
output.markdown(selectedTemp);

} else {
let record = await input.recordAsync(‘Select a record to use’, deals);
}

OUTPUT
Congrats you have been approved for: ${record.name}.

At lease signing we will need the following certified bank checks:

  • ${record.getCellValueAsString(“Price”)} made out to “${record.getCellValueAsString(“Entity (from Property Info)”)}” for the first month of rent
  • ${record.getCellValueAsString(“Price”)} made out to “${record.getCellValueAsString(“Entity (from Property Info)”)}” for the one month security deposit

Thanks,

OUTPUT I WANT
Congrats you have been approved for: 555 East 78th Street.

At lease signing we will need the following certified bank checks:

  • $5000 made out to “REAL ESTATE ENTITY” for the first month of rent
  • $5000 made out to “$REAL ESTATE ENTITY” for the one month security deposit

Thanks,

3 Replies 3

Welcome to the Airtable community!

It is also difficult to view your code. If you use the “Preformatted Text” formatting button in the forum editor, it will be easier to read your script.
It is also difficult to tell if you have included the entire script in your post or not. I do not see the code where you merge the record data with the template. You might have a quote issue (using string interpolation requires backticks), or it might be something else entirely.

It would also be helpful if you include screen captures of your template and record values in the grid view.

Hi,

This is the whole script:

// Change this name to use a different table

let deals = base.getTable(“Deals”);
let templates = base.getTable(“Templates”);

// Prompt the user to pick a record from deals then from templates
// If this script is run from a button field, this will use the button’s record instead.
output.text(“Templates matched with Records”)

let record = await input.recordAsync(‘Select a record to use’, deals);
let temp = await input.recordAsync(‘Select a template to use’, templates);
// You can use temp.getCellValue("Field name") to access the template
var selectedTemp = temp.getCellValueAsString(“written template”);
console.log(selectedTemp)

// Customize this section to handle the selected record
if (record) {
// cell values from the template which merges the record data with the template
output.markdown(selectedTemp);

} else {
let record = await input.recordAsync(‘Select a record to use’, deals);
}

NOTE:
The template is a record in my database that I am calling within the if statement that request the record data. If I past this into the if statement instead of calling it with output.markdown(selectedTemp); it works. But when I call it using the output.markdown(selectedTemp); function it passes it as a string and doesn’t perform the operations for the items in the template.

Are you saying that when you paste the template into the script, you get the expected output, but when you get the template from the base, you do not get the desired output?

This is because JavaScript will do string interpolation at runtime with a template literal that is hardcoded into the script.

For example these will output the record name,

output.markdown(`Congrats you have been approved for ${record.name}`)

or

let message = `Congrats you have been approved for ${record.name}`
output.markdown(message)

But this will not …

let message = template.getCellValue("written template")
output.markdown(message)

Technically, you could probably get this to do what you want with something like …

let templateString = template.getCellValue("written template")
let message = eval("`" + templateString + "`")
output.markdown(message)

However, I do NOT recommend this! This is a complicated way of doing things, makes your templates dependent on this specific script, and introduces security risks. In general, I recommend avoiding eval unless it is absolutely necessary. Using eval also will not work with automation scripts.

I have written many scripts that combine record data with a template stored in the base, and the templates use Mustache syntax. This is a very common templating system that is easy for most people to learn and use.