Deepak Negi
Mega Sage
Mega Sage

In the previous post, Multilingual Notifications- NOW made easy! - Now Platform - Article - ServiceNow Community

I was able to send out language respective notification to a single user, without creating multi-language notifications.

Now following the footsteps of it, I am able to build the functionality where it handles same notification to be sent out to multiple users without creating multiple notifications in different languages.

So this is what we need to do:

1. Event Registry:
Create a new event in System Policy > Events > Registry.

find_real_file.png

 

2. Notification:
Create a new Notification in System Notification > Email > Notifications. Check the Event Parm1 contains recipient field

 

 

find_real_file.png

 

3. Localization:

Create the language respective messages in System Localization > Messages

find_real_file.png

 

4. Business Rule for trigger

Create a BR on Incident Table to trigger the event. The whole logic lies in this script i.e to identify the available languages, recipients and notification Content.

(function executeRule(current, previous /*null when async*/ ) {

    var objArray = [current.caller_id.name, current.number]; // Replaces the variables in the email content
    var group = current.assignment_group;
    var languages = this.__getLanguages(); // get all languagues for all the Active users in the system
    var groupMembers = this.__getGroupMembers(group); // Get group members as the recipients in this case.
    var emailContent = this.__getContent(languages, groupMembers, objArray); // the content and the recipient to be pushed forward for notifications
    for (var k in emailContent)
        gs.eventQueue("dn.multilingual.notification", current, emailContent[k].recipient, JSON.stringify(emailContent[k]));
})(current, previous);

function __getGroupMembers(group) {
    var memObj = [];
    var memGr = new GlideRecord("sys_user_grmember");
    memGr.addQuery("group", group);
    memGr.query();
    while (memGr.next()) {
        var memProp = {};
        memProp.user = memGr.getValue('user');
        memProp.lang = memGr.user.preferred_language.toString();
        memObj.push(memProp);
    }
    return memObj;

}

function __getLanguages() {
    var langObj = [];
    langObj.push('en');
    var lang = new GlideAggregate("sys_user");
    lang.addAggregate("COUNT", "preferred_language");
    lang.addActiveQuery();
    lang.query();
    while (lang.next()) {
        if (lang.getValue("preferred_language") != "" && lang.getValue("preferred_language") != 'en')
            langObj.push(lang.getValue("preferred_language"));
    }
    return langObj;
}

function __getContent(lang, recipients, objArray) {
    var contentObj = [];
    for (var i in lang) {
        var emailContentObj = {};
        emailContentObj.subject = gs.getMessageLang("dn.notification.subject", lang[i]);
        emailContentObj.body = gs.getMessageLang("dn.notification.body", lang[i], objArray);
        emailContentObj.recipient = this.__getRecepient(lang[i], recipients);
        contentObj.push(emailContentObj);
    }
    return contentObj;
}

function __getRecepient(lang, recipients) {
    var recObj = [];
    for (var i in recipients) {
        if (recipients[i].lang == lang)
            recObj.push(recipients[i].user);
    }
    return recObj;
}

 

5. Email script for Dynamic Content

Create a new notification email script in System Notification > Email > Notification Email Scripts.

find_real_file.png

Script:

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {
			var cont = JSON.parse(event.parm2);
			email.setSubject(cont.subject);
			template.print(cont.body);

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

 

6. Refer this email script(4) in the notification created(2):

find_real_file.png

 

Thats it!

Use Case: Now I have used the Assignment group notification which will be sent out to the members of the Assignment group. I have a group inside which I have 2 member each with English and French Language.

 

Result:

English:

find_real_file.png

French:

find_real_file.png

The same notification is sent out to multiple users with different languages.

Please note:

1. This script is still in initial phase and require some optimization and modular adjustments.

2. I was able to successfully test for 1 member each for a language but there would be a need to modify the content generation function in BR if there are multiple users of same language.I would leave this part to you guys 🙂

3. The same notification can be utilized for multiple functionalities e.g watchlist, assigned to, group etc. However, the content needs to be managed accordingly either in the Messages table or a custom solution to store messages wrt language.

4. Open to any suggestions/feedback and comments. There can be much creative way to implement this. Please let me know about the same.

 

Thanks Guys!

Happy Learning 🙂

Regards

Deepak

Version history
Last update:
‎01-03-2022 05:06 AM
Updated by: