The CreatorCon Call for Content is officially open! Get started here.

Scheduled job restricting notifications for specific country

RenzVillanH
Kilo Contributor

Hello there,

 

Is it possible to restrict the notifications for users depends on the country?

 

COUNTRIES:

 'Spain', 'Portugal', 'France', 'Italy', 'Greece',
      'Slovenia', 'Croatia', 'Serbia', 'North Macedonia',
                    'Turkey', 'Andorra', 'Israel'


SOURCE CODE:

 

(function () {
    var currentDate = new GlideDateTime();
    var currentDateOnly = currentDate.getDate();

    var notificationTemplate = "Reminder: Computer Replacement Due in 3 Months – Please Order Soon";

    var gr = new GlideRecord('cmdb_ci_computer');
    gr.addNotNullQuery('u_5_years_in');
    gr.addQuery('chassis_type', 'IN', 'Desktop,Notebook');
    gr.query();

    while (gr.next()) {

        // Only notify Active and Shared Device statuses
        if (gr.install_status != 2 && gr.install_status != 110) {
            continue;
        }

        var u5YearsInValue = gr.getValue('u_5_years_in');
        if (!u5YearsInValue) {
            continue;
        }

        var user = gr.assigned_to;
        if (user) {
            var userGR = new GlideRecord('sys_user');
            if (userGR.get(user)) {
               
                if (userGR.user_name == 'test') {
                    continue;
                }

                // Skip users with u_authorization_level >= 8
                if (!gs.nil(userGR.u_authorization_level) && parseInt(userGR.u_authorization_level, 10) >= 8) {
                    continue;
                }
            }
        }

        // Calculate 3 months before EOL
        var endDate = new GlideDateTime(u5YearsInValue);
        endDate.addMonthsUTC(-3);
        var sixMonthsBeforeEOL = endDate.getDate();

        if (currentDateOnly.compareTo(sixMonthsBeforeEOL) === 0) {
            gs.eventQueue('cmdb_3months.before.eod', gr, 'The end of lifecycle for ' + gr.name + ' is approaching in 3 months.', notificationTemplate);
        }
    }
})();
 
Thank you
3 REPLIES 3

Bhuvan
Mega Patron

@RenzVillanH 

 

You can extend below logic

 

https://www.servicenow.com/community/developer-forum/is-it-possible-to-disable-email-notifications-f...

 

If this helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

Rafael Batistot
Kilo Patron

Hi @RenzVillanH 

 

You need add some like:

var restrictedCountries = [
'Spain', 'Portugal', 'France', 'Italy', 'Greece',
'Slovenia', 'Croatia', 'Serbia', 'NorthMacedonia', 'Turkey', 'Andorra', 'Israel'
];

var userCountry = userGR.getValue('country'); // country field name

//check the list of country
if (userCountry && restrictedCountries.indexOf(userCountry) > -1) {
continue;

 

important: The country value needs to be in the sys_user table

Ankur Bawiskar
Tera Patron
Tera Patron

@RenzVillanH 

how is the recipient set?

Is it directly at notification level in Who Will Receive tab?

Share notification configuration screenshots.

Users from above countries don't want your Reminder email or they don't want any Notification from instance?

💡 If my response helped, please mark it as correct ✔️ and close the thread 🔒 — this helps future readers find the solution faster! 🙏

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