Evaluate variables in a custom form field

rmoser
Tera Contributor

Hey all!

Not sure if this is a simple or complicated question, but our organization has a custom field in the Email Notification form called Banner Text(u_banner_text)

We use that for a custom banner in our email notifications like so:

    var bannerText = '';
    if (email_action.u_banner_text == '') {
        bannerText = email.subject;
    } else {
        bannerText = email_action.u_banner_text;
    }

So basically, if the field has a value, we use that - otherwise default to the email subject as the banner. This allows us to "beautify" emails that have a lengthy or data-heavy subject line, and keep the banner from looking awful.

 

Right now, if we enter a variable into the u_banner_text field, it displays as plaintext in the banner ( ${number} for example, displays exactly like that "${number}" rather than parsing the actual ticket number ), however if we leave banner blank, and use ${number} in the subject line, the banner will show the ticket number.

 

My question is - is there a way to make this custom field parse variables like the subject field?

ie: some method within the script to parse the variables, or some setting/permission the field needs to behave the same as subject

 

1 ACCEPTED SOLUTION

rmoser
Tera Contributor

Unfortunately neither method worked properly, I may have integrated it incorrectly, but by the looks of things, the templateFormatter and GlideTemplateEvaluator don't seem to work properly in emal scripts.

I did, however find a solution!

This may be an overcomplication of the solution, but it does work.

For anyone who happens to run into a similar requirement, what I did was extract the variable from the text

var regex = /\$\{([^}]+)\}/g;

resolved the field value directly on the current record

if (current[fieldName]) {
    fieldValue = current[fieldName];
}

then replaced the string

bannerText = bannerText.replace(fullMatch, fieldValue);

Here is the full code I used:

    // Process banner text with variable substitution
    var bannerText = '';
    if (email_action.u_banner_text == '') {
        bannerText = email.subject;
    } else {
        bannerText = email_action.u_banner_text;

        // Parse and replace syntax: ${fieldname}
        var regex = /\$\{([^}]+)\}/g;
        var matches = bannerText.match(regex);

        if (matches) {
            for (var i = 0; i < matches.length; i++) {
                var fullMatch = matches[i];
                var fieldName = fullMatch.substring(2, fullMatch.length - 1);
                var fieldValue = '';

                // Handle dot-notation field references
                if (fieldName.indexOf('.') !== -1) {
                    var parts = fieldName.split('.');
                    var obj = current[parts[0]];
                    if (obj && obj[parts[1]]) {
                        fieldValue = obj[parts[1]];
                    } else if (obj && typeof obj.getDisplayValue === 'function') {
                        fieldValue = obj.getDisplayValue();
                    }
                } else {
                    // Handle direct field references
                    if (current[fieldName]) {
                        fieldValue = current[fieldName];
                        // Use display value for reference fields
                        if (typeof current[fieldName].getDisplayValue === 'function') {
                            fieldValue = current[fieldName].getDisplayValue();
                        }
                    }
                }

                // Replace variable with resolved value
                bannerText = bannerText.replace(fullMatch, fieldValue);
            }
        }
    }

I hope this helps someone in the future! ðŸ˜Š

View solution in original post

2 REPLIES 2

YaswanthKurre
Giga Guru

Hi @rmoser ,

 

Try this script :

var bannerText = '';
var templateText = email_action.u_banner_text;

if (!templateText) {
    bannerText = email.subject;
} else {
    var evaluator = new GlideTemplateEvaluator();
    evaluator.setTemplate(templateText);
    evaluator.setContext(current); // Use 'current' to refer to the record the email is about
    bannerText = evaluator.evaluate();
}
OR gs.templateFormatter("Hello ${number}", current);

 

Mark this as helpful and correct if this helps you.

Thanks,

Yaswanth

 

rmoser
Tera Contributor

Unfortunately neither method worked properly, I may have integrated it incorrectly, but by the looks of things, the templateFormatter and GlideTemplateEvaluator don't seem to work properly in emal scripts.

I did, however find a solution!

This may be an overcomplication of the solution, but it does work.

For anyone who happens to run into a similar requirement, what I did was extract the variable from the text

var regex = /\$\{([^}]+)\}/g;

resolved the field value directly on the current record

if (current[fieldName]) {
    fieldValue = current[fieldName];
}

then replaced the string

bannerText = bannerText.replace(fullMatch, fieldValue);

Here is the full code I used:

    // Process banner text with variable substitution
    var bannerText = '';
    if (email_action.u_banner_text == '') {
        bannerText = email.subject;
    } else {
        bannerText = email_action.u_banner_text;

        // Parse and replace syntax: ${fieldname}
        var regex = /\$\{([^}]+)\}/g;
        var matches = bannerText.match(regex);

        if (matches) {
            for (var i = 0; i < matches.length; i++) {
                var fullMatch = matches[i];
                var fieldName = fullMatch.substring(2, fullMatch.length - 1);
                var fieldValue = '';

                // Handle dot-notation field references
                if (fieldName.indexOf('.') !== -1) {
                    var parts = fieldName.split('.');
                    var obj = current[parts[0]];
                    if (obj && obj[parts[1]]) {
                        fieldValue = obj[parts[1]];
                    } else if (obj && typeof obj.getDisplayValue === 'function') {
                        fieldValue = obj.getDisplayValue();
                    }
                } else {
                    // Handle direct field references
                    if (current[fieldName]) {
                        fieldValue = current[fieldName];
                        // Use display value for reference fields
                        if (typeof current[fieldName].getDisplayValue === 'function') {
                            fieldValue = current[fieldName].getDisplayValue();
                        }
                    }
                }

                // Replace variable with resolved value
                bannerText = bannerText.replace(fullMatch, fieldValue);
            }
        }
    }

I hope this helps someone in the future! ðŸ˜Š