The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Choice fields translated in email notifications

cornflakes_01
Giga Contributor

Hi All,
We're implementing language translation in our instance, and i'm undergoing email notification translations. Changing the email notifications based on the user's language is the easy part, but what I cannot figure out is how to display a translated version of the field value within a email template.

For example, on a change request the field called approval has translated versions of the choice options. When the ${approval} field is included within the email template, the value of that field is displayed in the system's default language, rather than the language of the user who is being sent the email.

How do I go about showing the translated version of that field based on the user's language in an email(or I could also hardcode the language that I want to display it in also)?

4 REPLIES 4

kedarnath13
Kilo Explorer

Hi

whenever you call ${approver} the Value of the field will be fetched(not the "Label" of choice..!!).so have a mail_script tag that will fetch label value depends upon recipients.

Or else if not choice labels are added,we can use sys_message table for internalization purpose.so use mail_script tag and GlideRecord to get values based up on language.

Note : Do not use gs.getMessage().in this case,getMessage will fetch values by user who triggered event but not who is the receiver .

Lemme know if you need more help.

Thanks


cornflakes_01
Giga Contributor

Hi Kedarnath,
Thanks for the response. I really wish that we didn't have to do that for any choice fields, and am surprised that Service-Now didn't account for this in some way. We'll proceed with this solution, but we'll be submitting an enhancement request to Service-Now to improve on translation of email notifications as this is way too tedious for my liking.

Thanks for your help,
Scott


cornflakes_01
Giga Contributor

Here's what we did to get translation of choice fields based on language.

throw this code below into a global business rule:



/* *******************************************************************
Function Name: getChoiceTranslation
Type: Global Business Rule
Inputs: cur (string), fieldName (String), languageCode (string), includeFieldLabel (boolean)
Output: translated (string)
Created By: cornflakes_01
Purpose:
The purpose of this function is to translate choice field values based on a specific language. The function looks up the value for the specified field and language, then returns the label for that value. If the includeFieldLabel variable is set to true, this function also looks up the translated label for the selected field and returns that in addition to the choice field label (format "<FIELD LABEL>: <CHOICE LABEL>")
******************************************************************* */

function getChoiceTranslation(cur, fieldName, languageCode, includeFieldLabel){

if (cur.getTableName() == 'sysapproval_approver'){
cur = cur.sysapproval.getRefRecord();
}
//get the base table of the field in question
var table = cur.getElement(fieldName).getBaseTableName();
//get the value of the field in question on the current record
var value = eval('cur.'+ fieldName);
//Query the choice table to find the label for the translated language
var gr = new GlideRecord('sys_choice');
gr.addQuery('name', table);
gr.addQuery('value', value);
gr.addQuery('element', fieldName);
gr.addQuery('language', languageCode);
gr.query();
if (gr.next()){
choiceLabel = gr.label;
}
else{
//If the query doesn't result in any records, the best you can do is return the value of the field in the base language
choiceLabel = cur.getElement(fieldName).getChoiceValue();
}
//if the include field label boolean is input as true, then find the label of the fieldName in teh same language
if (includeFieldLabel == 'true'){
var gr2 = new GlideRecord('sys_documentation');
gr2.addQuery('name', table);
gr2.addQuery('element', fieldName);
gr2.addQuery('language', languageCode);
gr2.query();
if (gr2.next()){
fieldLabel = gr2.label;
}
else{
fieldLabel = cur.getElement(fieldName).getLabel();
}
return fieldLabel + ': ' + choiceLabel;
}
else{
return choiceLabel;
}

}


And call this function from within the email notification or the email template using this code:


template.print(getChoiceTranslation(current, 'FIELD NAME', '2 LETTER LANGUAGE CODE', 'TRUE/FALSE') + '\n');


for example, this call


template.print(getChoiceTranslation(current, 'priority', 'fq', 'true') + '\n');


returned the following in the outbound email for a medium priority task:
Priorité: 3 - Modéré

Hope this helps others!


UPASANA2
Mega Expert

HI Scott,



How did you create email notifications based on the user's language?