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

moonlin
Kilo Explorer

Hi Dreon



I'm just starting to use ServiceNow since this March and maybe i'm the only Chinese guy to use this tool.



I just want to add the cu into the notification ony by one



Here is my variable set in English



var gr_ritm = new GlideRecord('sc_req_item');


if(gr_ritm.get(current.document_id)){


  template.print(gr_ritm.variables.monitor);



I get to know that i need to get the value from sys_translated, but i don't know how to prepare the scripts.



I tried with this but it shows incorrect results:


var retVal='';


var trans=new GlideRecord('sys_translated');


// trans.addQuery('documentkey', current.cat_item.sys_id);


trans.addquery('value','Compact laptop');


trans.addQuery('Element','question_text');


trans.addQuery('language', 'zh');


trans.query();


while (trans.next()){


  retVal=trans.label;


}


template.print(retVal)


Max20
ServiceNow Employee
ServiceNow Employee

Hi Moon LIN,


have you tried replacing the row


trans.addQuery('Element','question_text');


with


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


?


The field names should be case sensitive..


caruofor
Kilo Contributor

Hi Dreon,



I have followed your suggestions the only thing I changed was I wanted Polish as opposed to Italian.



I triggered an email notification, this returned a blank email. Would you have any advice ?



Kind regards,



Chris