```const template = render.mergeEmail({ templa...
# suitescript
c
Copy code
const template = render.mergeEmail({
    templateId: 37,
    transactionId: Number(transactionData.transactionId)
})

const emailSendOptions = {
    author: senderId,
    recipients: recipientEmail,
    subject: template.subject,
    body: template.body,
    relatedRecords: {
        transactionId: transactionData.transactionId
    }
}

email.send(emailSendOptions);
I have a list of item names from a saved search, is there a way to pass that list into this email template?
a
There's 2 approaches here... 1. Make use of the templateRenderer object which is another part of the N/render module. Once you have a templateRenderer object you can use the addSearchResults method to pass those into the template 2. if you can just get away with adding the raw text from your item search results to the end of the body of the email you can just add the item string to template body before doing the email send.
Copy code
const emailBody = template.body + itemNames.toString()
assuming your itemNames are already in an array 🙂
c
I'll take a look at both options, thanks.
👍 1
b
To expand on the second approach, if you'd rather not place the list of items at the end of the email, you can adjust the template by adding a placeholder keyword in the body text. Then, you can replace that keyword with your list of items. A simple way to do this is by using Handlebars notation, like {{itemsList}}.
c
{{itemNames}} just passes through as a string ${{itemNames}} gives an error and if I change ${transaction.itemNames} to ${itemNames} I get an error "The following has evaluated to null or missing: ==> itemNames"
Copy code
function sendTemplatedEmail(transactionData) {
    const senderId = 47165;
    const recipientEmail = transactionData.customerEmail;
    let itemNames = transactionData.items.map(item => item.itemDisplayName).join(", ");

    const template = render.mergeEmail({
        templateId: 37,
        transactionId: Number(transactionData.transactionId)
    })

    const emailSendOptions = {
        author: senderId,
        recipients: recipientEmail,
        subject: template.subject,
        body: template.body + itemNames.toString(),
        relatedRecords: {
            transactionId: transactionData.transactionId
        }
    }

    email.send(emailSendOptions);

    // updateSentEmailLines(transactionData)
}
<p> Item(s) no longer available: ${itemNames} </p>
b
The idea here is to let {{itemNames}} pass as a string so you can replace the string within your script for the list of items
c
Copy code
body: template.body.replace('##ITEM_NAMES##', itemNames),
I was just trying this
Maybe I'm still being naive
{{itemNames}} literally passed as {{itemNames}}
.replace didn't work at all, the email didn't even send.
b
That would work, it's the same thing you can do body.replace('{{itemNames}}', itemNames.toString()) or the one with ## would also work. I just chose handlebars notation cause that's popular
c
I',m not clear on what you're suggesting
b
Did you stringify itemNames?
c
No, I'll try that
Copy code
let itemNames = transactionData.items.map(item => item.itemDisplayName).join(", ");
const newItemNames = json.stringify(itemNames);
b
Handlebars is a templating engine, the syntax for the variables is {{nameofvariable}} , it's very common
c
I built the template using handlebars
so I'm somewhat familar
If I don't use a template, this is easy
I can pass my data straight into the email by returning the email as a string `` with ${{fieldName}} for the fields I want to pass from the search
b
Glad you were able to make it work!
c
It doesn't work lol
I can only make it work without the template
I'm still testing the stringify addition
Another option could be to write the HTML straight into the SutieScript then I can bypass the NS template system all together. That's not user friendly though.
b
Give it a try, the email should send or throw an error. if it sends without replacing the {{itemNames}}, make sure the string you are passing is in HTML mode, if you use the WYSIWYG mode it may add some extra characters to the string
c
Copy code
let itemNames = transactionData.items.map(item => item.itemDisplayName).join(", ");
const newItemNames = JSON.stringify(itemNames);
Copy code
body: template.body.replace('{{newItemNames}}', newItemNames),
Do you think this should work?
b
does your template has {{newItemNames}} text somewhere in the body?
c
p Item(s) no longer available: ${{newItemNames}} /p
b
remove the $, it should be just {{newItemNames}}
c
ok
I will try that
All working with passing the list in as a string. Do you think it's possible to pass an array so I can iterate through the items in freemarker?
b
Yeah, I think that might be possible. I don't play around with it often but I think you can attach a whole record even. You'll have to experiment.
👍 1