Email notification translation using sys_ui_message

soumya17
Tera Contributor

Hi, I want to translate the email content automatically based on incident caller language.

below approach i followed.

I created a sys_ui_message and i have called message in email script, then i have added the mail script in notification body.
for e.g: sys_ui_message:

key- email.greeting 
message - hi - in english
Buongiorno - in italian

similarly i created all messges, below is the email script i called.
but when i raise incident with caller language as italian language, the notification content showing still in english instead of Italian content.
can anyone help..


Thanks for your feedback.

(function runMailScript(current, template, email, email_action, event) {
 
    var lang = 'en';
    var callerName = '';
 
    if (current.caller_id) {
        var caller = current.caller_id.getRefRecord();
        if (caller.isValidRecord()) {
            callerName = caller.getDisplayValue();
 
            if (caller.preferred_language) {
                var langRec = caller.preferred_language.getRefRecord();
                if (langRec.isValidRecord() && langRec.code) {
                    lang = langRec.code.toString();
                }
            }
        }
    }
        var session = gs.getSession();
        var originalLang=session.getLanguage();
        session.setLanguage(lang);
    function msg(key, lang, params) {
        return gs.getMessageLang(key, lang, params || []);
    }
 
    var baseStyle = 'font-family:Arial; font-size:18px;';
    var headingStyle = 'font-family:Arial; font-size:22px; font-weight:bold; text-align:center';
 
    // šŸ”¹ Main heading
    template.print('<p style="' + headingStyle + '">' + msg('incident.recorded') + '</p>');
  template.print              ('_________________________________________________________________________________________');
    // šŸ”¹ Greeting
    template.print('<p style="' + baseStyle + '">' + msg('email.greeting', current.caller_id.getDisplayValue()) + '</p>');
 
    // šŸ”¹ Intro
    template.print('<p style="' + baseStyle + '">' + msg('incident.details.intro') + '</p>');
 
    // šŸ”¹ Bullet points
    template.print(
        '<ul style="' + baseStyle + ' list-style-type:disc; padding-left:20px;">'
    );
 
    template.print('<li><b>' + msg('incident.number') + ':</b> ' + current.number + '</li>');
    template.print('<li><b>' + msg('incident.created') + ':</b> ' + current.sys_created_on.getDisplayValue() + '</li>');
    template.print('<li><b>' + msg('incident.caller') + ':</b> ' + callerName + '</li>');
    template.print('<li><b>' + msg('incident.shortdesc') + ':</b> ' + current.short_description + '</li>');
 
    template.print('</ul>');
 
    // šŸ”¹ Incident link
    var url = gs.getProperty('glide.servlet.uri') + current.getLink();
    template.print(
        '<p style="' + baseStyle + '"><a href="' + url + '">' +
        msg('incident.link') +
        '</a></p>'
    );
    session.setLanguage(originalLang);
 
})(current, template, email, email_action, event);
1 ACCEPTED SOLUTION

lauri457
Tera Sage

preferred_language is a string not a reference record

lauri457_0-1769574622560.png

You can just get the string value

if (caller.preferred_language) {
    /*var langRec = caller.preferred_language.getRefRecord();
    if (langRec.isValidRecord() && langRec.code) {
        lang = langRec.code.toString();*/
    lang = caller.getValue("preferred_language")
}

 And because you don't pass a lang argument in your msg function call your local scope shadows the global lang variable and will be undefined:

function msg(key, /*lang,*/ params) {
    return gs.getMessageLang(key, lang, params || []);
}

 

You could also include the html tags in the ui message and include styling in the notification/layout itself. However, working example below:

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {
	var caller = current.caller_id.getRefRecord(); 
	var lang = caller.getValue("preferred_language"); 
	var callerName = caller.getDisplayValue(); 
	function msg(key, params) {
		return gs.getMessageLang(key, lang, params || []);
	}

	var baseStyle = 'font-family:Arial; font-size:18px;';
	var headingStyle = 'font-family:Arial; font-size:22px; font-weight:bold; text-align:center';

	// :small_blue_diamond: Main heading
	template.print('<p style="' + headingStyle + '">' + msg('incident.recorded') + '</p>');
	template.print('_________________________________________________________________________________________');
	// :small_blue_diamond: Greeting
	template.print('<p style="' + baseStyle + '">' + msg('email.greeting', current.caller_id.getDisplayValue()) + '</p>');

	// :small_blue_diamond: Intro
	template.print('<p style="' + baseStyle + '">' + msg('incident.details.intro') + '</p>');

	// :small_blue_diamond: Bullet points
	template.print(
		'<ul style="' + baseStyle + ' list-style-type:disc; padding-left:20px;">'
	);

	template.print('<li><b>' + msg('incident.number') + ':</b> ' + current.number + '</li>');
	template.print('<li><b>' + msg('incident.created') + ':</b> ' + current.sys_created_on.getDisplayValue() + '</li>');
	template.print('<li><b>' + msg('incident.caller') + ':</b> ' + callerName + '</li>');
	template.print('<li><b>' + msg('incident.shortdesc') + ':</b> ' + current.short_description + '</li>');

	template.print('</ul>');

	// :small_blue_diamond: Incident link
	var url = gs.getProperty('glide.servlet.uri') + current.getLink();
	template.print(
		'<p style="' + baseStyle + '"><a href="' + url + '">' +
		msg('incident.link') +
		'</a></p>'
	);

})(current, template, email, email_action, event);

 

View solution in original post

11 REPLIES 11

Hi @Ankur Bawiskar ,

 

Thanks for your response.. I tried but no luck. 

I have changed the email script , and i raised incident with caller whose language is 'Italian'
But still the email triggered in english.. below is the script i wrote.

(function runMailScript(current, template, email, email_action, event) {

    var lang = 'en'; // Default
    if (current.caller_id) {
        var caller = current.caller_id.getRefRecord();
        if (caller.isValidRecord() && caller.preferred_language) {
            var langRec = caller.preferred_language.getRefRecord();
            if (langRec.isValidRecord() && langRec.code) {
                lang = langRec.code.toString();
            }
        }
    }

    // Define msg function
    function msg(key, lang) { // Remove lang param; use session or getMessageLang(lang)
        return gs.getMessageLang(key, lang);
    }
 
    var baseStyle = 'font-family:Arial; font-size:18px;';
    var headingStyle = 'font-family:Arial; font-size:22px; font-weight:bold; text-align:center';
 
    // šŸ”¹ Main heading
    template.print('<p style="' + headingStyle + '">' + msg('incident.recorded',lang) + '</p>');
  template.print              ('_________________________________________________________________________________________');
    // šŸ”¹ Greeting
    template.print('<p style="' + baseStyle + '">' + msg('email.greeting', lang) + '</p>');
    //template.print('<p style="' + baseStyle + '">' + msg('email.greeting',lang, current.caller_id.getDisplayValue()) + '</p>');
 
    // šŸ”¹ Intro
    template.print('<p style="' + baseStyle + '">' + msg('incident.details.intro',lang) + '</p>');
 
    // šŸ”¹ Bullet points
    template.print(
        '<ul style="' + baseStyle + ' list-style-type:disc; padding-left:20px;">'
    );
 
    template.print('<li><b>' + msg('incident.number',lang) + ':</b> ' + current.number + '</li>');
    template.print('<li><b>' + msg('incident.created') + ':</b> ' + current.sys_created_on.getDisplayValue() + '</li>');
    template.print('<li><b>' + msg('incident.caller') + ':</b> ' + callerName + '</li>');
    template.print('<li><b>' + msg('incident.shortdesc') + ':</b> ' + current.short_description + '</li>');
 
    template.print('</ul>');
 
    // šŸ”¹ Incident link
    var url = gs.getProperty('glide.servlet.uri') + current.getLink();
    template.print(
        '<p style="' + baseStyle + '"><a href="' + url + '">' +
        msg('incident.link') +
        '</a></p>'
    );
    session.setLanguage(originalLang);
 
})(current, template, email, email_action, event);

@soumya17 

did you debug by adding gs.info() and see what came?

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

Hi @Ankur Bawiskar ,

yes, logs show in english..below is simple log i have added..

gs.log('Hi INc Recorded'+gs.getMessage('incident.recorded',lang));
soumya17_0-1769094038114.png

 

 

@soumya17 

please share the screenshots for your UI messages.

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

Hi @Ankur Bawiskar ,

 

Can you suggest any other solution?

 

Thanks