- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2017 03:23 AM
Hi everyone,
I have a requirement where I have run a fix script to set the state of Risk records that has #closed tag. This table has an email notification set up which will be triggered if the record is closed.
As this is an adhoc process, I need to ensure that the notification is not triggered once I run the Fix Script.
Is there a way to accomplish this without de-activating the notification?
Thanks
Regina
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2017 03:27 AM
Hi Regina,
If the emails are triggered via event, from a business rule. then can you use setWorkflow(false) in your fix script which will not let the BRs to run and hence no email.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2017 06:06 AM
You cannot disable "some of the business rules" with setWorkflow(false). If you leave it true, you will trigger notifications. If you set it to false, you'll miss that logic in your BR. The only solution I can see is to build that logic from the BR in to your fix script. It means that you'll need to keep track of current and previous values yourself since current and previous objects are not available in Fix Scripts.
http://wiki.servicenow.com/index.php?title=Fix_Scripts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2017 07:12 AM
Thanks Chuck for the reply.
I'm not sure how building the logic in the BR would stop the notification from being triggered. Could you expand this a bit please.
Right now this is what I have in my Fix Script:
I am basically de-activating the notification that's being triggered when the state is set to Closed. For the example below it is straight forward as there is only one notification that's being triggered. But there's a chance that there will be multiple notifications in other cases. I just wonder if my solution below is considered best practice?
Also downside is of using this is that I can only run this outside business hours to ensure no process is being impacted as I am de-activating notification...
Code:
//Let's de-activate the notification first
setMailNotificationActive(false);
var riskRec = new GlideRecord('u_risk');
//proces risk records
riskRec.addEncodedQuery('u_tagLIKE#closed^state!=Closed');
riskRec.query();
while(riskRec.next()) {
gs.log('sysid-' + riskRec.sys_id)
riskRec.state = 'Closed';
riskRec.u_work_notes = 'Risk automatically closed according to its heritage risk register state'
riskRec.update();
// break;
}
//Once all records were updated, activate the notification again
setMailNotificationActive(true);
function setMailNotificationActive(myaction) {
var grMail = new GlideRecord("sysevent_email_action");
grMail.addEncodedQuery('active=true^name=Risk: Risk closed');
grMail.query();
while(grMail.next()) {
grMail.active = myaction;
grMail.update();
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2017 07:25 AM
It looks solid enough. Just note, that when you go to their them back on, this will not get your deactivated notifications...
grMail.addEncodedQuery('active=true^name=Risk: Risk closed');
You'll likely be better with something like:
function setMailNotificationActive(myaction) {
var grMail = new GlideRecord("sysevent_email_action");
if (grMail.get('name', 'Risk: Risk closed')) {
grMail.active = myaction;
grMail.update();
}
}
I'm going to assume, there is only one notification with that name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2017 07:52 AM
Thanks Chuck for the code. One thing I've noticed though is that when I turn the notification back on, the emails were sent / triggered.. I am hoping that it won't as I already done the update prior to activating it. Am I missing something?
//Once all records were updated, activate the notification again
setMailNotificationActive(true);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2017 04:26 PM
That's odd. It could be that the events are being queued, but not processed right away (which can happen) then you turn the notification back to active, the events get processed and notifications get sent.
You would need to know when the events are done being processed. The only way I can think of doing this is to create an event in the registry called sysevent_email_action.risk.active and instead of calling your setMailNotificationActive() function to re-activate it (possibly while the other events are still in queue), trigger the new event sysevent_email_action.risk.active, then use your script info in a Script Action that responds to that event. Theoretically, the other events should be processed, no notifications triggered, then it gets to process sysevent_email_action.risk.active, triggers the Script Action and re-activates the notification record. It's worth a try.
Script Actions - ServiceNow Wiki
Script Actions - Business Rules' cousin