send email with dynamic data

YounseoC
Tera Contributor

Hi, I'm trying to send an email with dynamic data.

 

I made a task with 'Email template', 'Email subject', and 'Email body' fields. When a user selects an email template from the 'Email template' field, 'Email subject' and 'Email body' field will auto-populate with the email template's subject and body values.

When the task is closed complete, it will trigger a flow that sends an email with the task's 'Email subject' and 'Email body' field values.

Email body contains dynamic data such as ${parent.subject_person.name}. I expected the query to get the date from task record, but when I checked the sent email, the dynamic data querys were printed as it is.

 

How can I make the query work? and at which point should the query be substituted with actual data?

6 REPLIES 6

kaustubh Desai
Tera Guru

Hello @YounseoC , 

Can you please share the code you are using to populate the data in your email body?



If you found my response helpful, please mark it as correct and close the thread to benefit others.
Regards,
Kaustubh

Ankur Bawiskar
Tera Patron
Tera Patron

@YounseoC

it won't be evaluated on the fly.

It won't work that way

Please share entire configuration script etc for this.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

YounseoC
Tera Contributor

YounseoC_0-1741068617990.png

 

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    console.log('Triggering GlideAjax for sys_id: ' + newValue);
    var ga = new GlideAjax('GetEmailTemplateData');
    ga.addParam('sysparm_name', 'getEmailTemplate');
    ga.addParam('sysparm_u_email_template', newValue);
    ga.getXMLAnswer(function(response) {
        try {
            if (response) {

                var emailTemplate = JSON.parse(response);
                console.log('Parsed emailTemplate:', emailTemplate);

                g_form.setValue('u_email_body', emailTemplate.body || '');
                g_form.setValue('u_email_subject', emailTemplate.subject || '');

                console.log('client script works');
            } else {
                console.error('Error: No response received from Script Include');
            }
        } catch (e) {
            console.error('Error parsing response: ', e);
        }
    });
}

YounseoC
Tera Contributor