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.
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.
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.