Auto add users to Services for outage notifications

Moedeb
Tera Guru

I have a portal page that can show system outages, on that page our users can subscribe to a service so that if there is any sort of outage/degradation they will receive an email notification about what is happening.

 

This has always been a manual process to add users to a service and have them go to unsubscribe if they decided they didn't want to receive notifications for any particular service.

The page looks like this:

Moedeb_0-1677635274800.png

 

What it looks like on the Service record in the ITSM module is:

Moedeb_1-1677635502527.png

Those people who are listed on the service under "Users" are the subscribed users and they can be manually added via the "edit" option and chosen from the list. 

 

What I want to do is to have new staff created "today" auto added to the "user" part of each of the services we have on our portal page (which is not every service) and have them be able to go and unsubscribe later if they wish, but by default all new users get auto subscribed to each of these services.

I'm think a business rule, but really don't know how to create one for this, is someone able to show me how it should be setup?

1 ACCEPTED SOLUTION

Ok, good, glad it's going. To scale this, yes, you could add a comma separated list to a system property that you create of all the services that they should get auto added to.  System property should be string type and have a value like sys_id1,sys_id2,sys_id3

 

Then in your script, you can call it in like:

 

var services = gs.getProperty('name_of_property');
var array = services.toString().split(',');
for (var i = 0; i < array.length; i++) {
var gr = new GlideRecord('m2m_sp_status_subscription');
gr.initialize();
gr.cmdb_ci_service = array[i];
gr.sys_user = current.sys_id;
gr.insert();
}

 

So that should iterate through the common separated list and add them to each service. This method avoids any customization, but another way could be that you add a checkbox field to the service form and call it: "Auto-Subscribe" or something like that and then check that box on those services, then use a GlideRecord query to query those services for that checkbox to be true and while loop through them using a script similar to the above except the array[i] would be the service sys_id, such as gr.sys_id if you used gr for your GlideRecord query (see my original reply with the link to the documentation).


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

11 REPLIES 11

Allen Andreas
Administrator
Administrator

Hi,

You can add a business rule (or flow designer flow) on the user table when the record is "created"/"inserted" and then in script add them. An example of just one would be something like:

var gr = new GlideRecord('name_of_table');
gr.initialize();
gr.user = current.sys_id;
gr.insert();

You can approach this from a number of ways as far as figuring out the services, whether that's an array of all the tables and you loop through them to execute the script on many tables adding them to it, or you can query your services (such as email) and then when found, execute script to add them, etc. That's up to you.

Here's GlideRecord API documentation as well to assist: https://developer.servicenow.com/dev.do#!/reference/api/rome/server_legacy/c_GlideRecordAPI#r_GlideR... 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Thank you for your response @Allen Andreas , not sure how I would execute this though so it updates the service with the new user created, for example:

Service = Email

User = Fred Lindy

Service Identifier to appear on the system status list = used_for is service_status (table cmdb_ci_service

 

Your example code says "name of table", but which table? Obviously the user is on the sys_user table and the Email service is in the cmdb_ci_service table. So how would I make a business rule add the new user to the email service?

 

For example business rule:

Name: add user service

Table: sys_user

Active: true

advanced: true

 

When to run:

When: after

Insert: true

order: 100

Filter:

Created on today

 

Advanced:

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

	// Add your code here
	var gr = new GlideRecord('name_of_table');
	gr.initialize();
	gr.user = current.sys_id;
	gr.insert();

})(current, previous);

 

How do I make that work?

Hi,

The "Users" related list in your screenshot is associated to data on a table...somewhere. You'd want to make an entry on that table, thus...the "name of table". If you right-click on Service = Email in the same place in your screenshot and choose "open in new window"...look at the URL and you'll see the table name. Your reply is a bit confusing so it's either that I'm not clear on what table you have these users associated to this service...or perhaps you may not know about the table relationship?

 

Looking in my instance, it appears to be the: "m2m_sp_status_subscription" table.

That table has fields "Service" (cmdb_ci_service) and "User" (sys_user).

So the script I provided would need to be adjusted using this information. Use the table name, set fields as appropriate. You'd want to set the service to the sys_id of the Email service record and the User to the sys_id of the current user, like I did in the example script.

 

Repeat as necessary for the other services you want to add them to.

 

I'm not sure what you mean by this line: "Service Identifier to appear on the system status list = used_for is service_status (table cmdb_ci_service"

 

Either way, I think I've explained a good portion of it to where it makes sense now?

 

 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Sorry if I'm not making sense @Allen Andreas , I am aware of the relationships, but wouldn't say I 100% understand them, but I do have an idea of them and how they work.

 

I remembered that I have a widget that does the subscription for all users when someone is on the page and presses the button, so I've tried to put that into the business rule in the way you described originally, but still haven't managed to get it to work.

I was hoping I'm close and you might be able to just finish off what I'm missing for me?

So I have a business rule:

Moedeb_0-1677648668835.png

With this script:

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

    // Add your code here
    var newSub = new GlideRecord("m2m_sp_status_subscription");
    newSub.addQuery("sys_user", gs.getUserID());
    newSub.addQuery("cmdb_ci_service", data.toSubscribe[j]);
    newSub.query();
    if (!newSub.next()) {
        newSub.sys_user = gs.getUserID();
        newSub.cmdb_ci_service = data.toSubscribe[j];
        newSub.insert();
    }

})(current, previous);