The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Email Notification

Swebb
Tera Contributor

I have setup an email notification in ServiceNow that is intended to send a message to the end user specific end user and a the person selected in a variable field called 

preceptor. To accomplish this, I configured an Email Notification, Event Registry, and a Scheduled Job, as the email needs to be sent 7 days before the Scheduled End Date on the request.

 

I ran a few tests and checked the email logs, but unfortunately, no emails were sent to end user or the user identified in the preceptor field. 

 

I've attached screenshots of my configuration for your review.

 

Script being used via Scheduled Jobs

 

var gr = new GlideRecord('sc_req_item');  
gr.addActiveQuery();
gr.query();

var targetDate = new GlideDateTime();
targetDate.addDays(7);
var targetDateStr = targetDate.getLocalDate().getValue();  // YYYY-MM-DD

while (gr.next()) {
  // assuming end_date is a variable on the RITM record
  var endDate = new GlideDateTime(gr.variables.end_date);
  var endDateStr = endDate.getLocalDate().getValue();  // normalize to YYYY-MM-DD

  if (endDateStr === targetDateStr) {
    gs.eventQueue(
      'custom.notify.manager.nonemployee',
      gr,
      gr.preceptor.toString(),  // send only preceptor sys_id
      null,
      null
    );
  }
}
3 REPLIES 3

YaswanthKurre
Tera Guru

Hi @Swebb ,

 

Make sure you are only sending two parameters ( I see you are having 3 with null). eventQueue() only supports two parameters for substitution

gs.eventQueue(
      'custom.notify.manager.nonemployee',
      gr,
      gr.request.requested_for.toString(),  // end user
      gr.variables.preceptor.toString()     // preceptor
    );

 

Mark this as helpful and correct if this helps you.

 

Thanks,

Yaswanth

 

VikMach
Mega Sage

@Swebb, your "IF Condition" is failing... See snip below. (endDateStr === targetDateStr) is returning false. Replace (Strict Equality Operator ===) with (Equality Operator ==) and see if that works???
One side note though, I would suggest to use Flow Designer. You have low code approach for this and you wouldn't need to create events and all this extra configuration. Everything will come in one place handy. Easy to debug and in future if any one wants to modify, debug or make any sort of changes would be easier. (I leave the choice to you.)

Note : As a best practice always have a condition before running any while loop in the scheduled job. If anything goes wrong in your script it will hamper the performance of the system. Have a "try - catch" method and "else" condition whenever using "if" to find out what's running and catch the error. This is to stop any unwanted operation that could run endlessly in unsupervised script execution.
Remember - "Running freeform script can cause system disruption or loss of data."



VikMach_0-1758295669289.png


Hope this helps.
Let me know if it worked.

Regards,
Vikas K

Rafael Batistot
Kilo Patron

Hi @Swebb 

In your script you’re calling:

var endDate = new GlideDateTime(gr.variables.end_date);

 

Problem: variables is only available in Catalog Client Scripts / Flow / Server-side processing but not directly accessible in a GlideRecord like that.
For catalog items, the RITM (sc_req_item) stores answers in sc_item_option and sc_item_option_mtom tables.

You can’t do gr.variables.end_date directly in a Scheduled Job script.



Fix: Use new GlideappVariablePoolQuestionSet() API or query sc_item_option table to pull the variable value.

Code exemple:

var itemVars = new GlideappVariablePoolQuestionSet();
itemVars.setRequestID(gr.sys_id);
itemVars.load();
var endDateVar = itemVars.getQuestion('end_date'); // replace with the variable name
var endDate = new GlideDateTime(endDateVar.getValue());

 

Same issue in gr.preceptor doesn’t exist as a field on sc_req_item.

If preceptor is a variable, you need to retrieve it with the same method as above.


instead of:

var preceptorVar = itemVars.getQuestion('preceptor');
var preceptorSysId = preceptorVar.getValue();


Use: 

gs.eventQueue(
  'custom.notify.manager.nonemployee',
  gr,
  preceptorSysId,
  null,
  null
);