Variables translated in notifications

Max20
ServiceNow Employee
ServiceNow Employee

Hi all,

I found a way to have variables (from Service Catalog) translated in notifications.

I created a script include:

var My_EmailFormatter = Class.create();

My_EmailFormatter.prototype = {

  initialize: function() {

  },

  compareVariables : function(a,b) {

            if (a.order < b.order)

                      return -1;

            if (a.order > b.order)

                      return 1;

            return 0;

  },

  getTranslated : function(text, language){

            var gr2 = new GlideRecord('sys_translated');

            gr2.addQuery('value', text);

            gr2.addQuery('element', 'question_text');

            gr2.addQuery('language', language);

            gr2.query();

            if (gr2.next()){

                      return gr2.label;

            } else {

                      return text;

            }

  },

  getTranslatedText : function(sysid, fieldName, text, language){

            var gr2 = new GlideRecord('sys_translated_text');

            gr2.addQuery('documentkey', sysid);

            gr2.addQuery('fieldname', fieldName);

            gr2.addQuery('language', language);

            gr2.query();

            if (gr2.next()){

                      return gr2.value;

            } else {

                      return text;

            }

  },

  type: 'My_EmailFormatter'

};

Then I created the email script "get_all_variables_translated" for approval notification:

var lang = 'it';

var mf = new My_EmailFormatter();

var item = new GlideRecord("sc_req_item");

item.get(current.sysapproval.sys_id);

item.query();

while(item.next()) {

  template.print('<dl class="dl-horizontal">');

  var set = new GlideappVariablePoolQuestionSet();

  set.setRequestID(item.sys_id);

  set.load();

  var vs = set.getFlatQuestions();

  for (var i=0; i < vs.size(); i++) {

            if(vs.get(i).getLabel() != '' && vs.get(i).getDisplayValue() != '') {

                      template.print('<dt>' +   mf.getTranslated(vs.get(i).getLabel(),lang) + ':</dt>');

                      template.print('<dd>' + vs.get(i).getDisplayValue() + '</dd>');

            }

  }

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

}

You can call the script from Notification HTML body using

${mail_script:get_all_variables_translated}

You can get the language dinamically if you want.

Hope this helps.

Bye

PS: Please mark Helpful, Like or Correct answer

7 REPLIES 7

madanm7786
Mega Guru

Hi Dreon,



Thanks for this post.


The scripts works fine, but the issue is irrespective of the user language, variables are populating in french language.



In one of our group approvals, there are users with different language like English, French, German. For all the users variables are displaying in french language.



Can you please help me to get the language dynamically.




Thanks,


Maddy


madanm7786
Mega Guru

Hi Dreon,



My bad, i identified the issue and fixed it, Everythigng is working fine now.



Thanks,


Maddy


Max20
ServiceNow Employee
ServiceNow Employee

You're welcome


Sue Frost
Giga Guru

Thanks for posting this idea / solution.



It solves a problem I'm looking at. We have an online form that will eventually generate a PDF. Our wrinkle is that a user can be working in one language and request a print in another.



I'll be using this to also access the Choice List table (sys_choice) to pull the appropriate label for a field value. This gives me a clean UI for the user in their preferred language and, with the addition of a "print language" field, allows me to generate the print correctly as well.