How to get variable data into a notification triggered by an event

User163016
Tera Contributor

I have a requirement to send a notification to a manager 10 days before their employee will term:

I wrote the following script for a Scheduled Script Execution to run everyday:

var gdt = new GlideDateTime();
    gdt.addDays(10);

var td = new GlideRecord('sys_user');
td.addQuery('u_termination_date', gdt);
td.query();
while (td.next()) {     
    gs.eventQueue ("tenday.term.notification", current, td.manager.email, td.name);
}


This registers an event, which inturn kicks off the notification.  There a 3 pieces of information that I need from the user record, the manager's email, that is in Parm1, the user's name, in Parm2, but the third one, termination date I can't populate.

The notification is built off of the sys_user table, but because the notification is triggered from an event and not an actual change to the table, it's not identifying the correct record so ${u_termination_date} is always blank.

Any ideas on how to direct the notification to the correct user record to pull the termination date?

1 ACCEPTED SOLUTION

Brent Sutton
Mega Sage

Hi Chad,

Add all your parameters to a JSON object and pass it in parameter 1:

Add value/pairs to variable in a JSON object.

var details = {"manager": td.manager.email, "name": td.name, "termination_date" : your_termination_field};

Refer to object in Email Script:

//extract details from JSON object
var firstName = event.parm1.manager.toString();
var lastName = event.parm1.name.toString();
var address = event.parm1.termination_date.toString();

//print results
template.print(manager);
template.print(name);
template.print(termination_date);

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.

View solution in original post

6 REPLIES 6

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi,

try to check the following thread, it will give an example on how to pass also the u_termination_date as parameter.

Can I create additional parameters on Events I create?

In details:

Just need to pass all your different parameters in an array, check the example below:

var commaSepArray = "banana,apple,orange";
gs.eventQueue(t"riiger.this.event", current, commaSepArray, gs.getUserID());

Then on your notification, use .split(",") to create an array of the values.

Hope this will fit your need.

If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.

Thank you

Cheers
Alberto

Raghu Loganatha
Kilo Guru

Hi, 

You can get this done in two ways, 

First method: You can trigger the event from schedule job and then write a notification email script where you can query the record and get all the information you need and then print it. 

Second method: Pass both manager email and user name in same parameter separating them by a delimiter( Like ":"). 

var gdt = new GlideDateTime();
    gdt.addDays(10);

var td = new GlideRecord('sys_user');
td.addQuery('u_termination_date', gdt);
td.query();
while (td.next()) {     
	var parameter1;
	parameter1 = td.manager.email+":"+td.name;
    gs.eventQueue ("tenday.term.notification", current, parameter1, td.u_termination_date);
}

Let me know if you have any further questions or mark this thread as answered. 

Brent Sutton
Mega Sage

Hi Chad,

Add all your parameters to a JSON object and pass it in parameter 1:

Add value/pairs to variable in a JSON object.

var details = {"manager": td.manager.email, "name": td.name, "termination_date" : your_termination_field};

Refer to object in Email Script:

//extract details from JSON object
var firstName = event.parm1.manager.toString();
var lastName = event.parm1.name.toString();
var address = event.parm1.termination_date.toString();

//print results
template.print(manager);
template.print(name);
template.print(termination_date);

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.

Thanks Brent.  Your solution worked out for me.  There was a few things I had to change.  From my past experience, I created the JSON object a little different:

var gdt = new GlideDateTime();
    gdt.addDays(10);
gs.print("TERM DATE: " + gdt);

var td = new GlideRecord('sys_user');
td.addQuery('u_termination_date', gdt);
td.query();
while (td.next()) {     
    var details = {};
	details.name = td.name.toString();
	details.term_date = td.u_termination_date.toString();
	var jsn = new JSON().encode(details);
    gs.eventQueue ("tenday.term.notification", td, td.manager.email, jsn);
}

I also had to do the it slightly different to pull the value pairs from the JSON in the email script:

//extract details from JSON object	
var str = event.parm2;
var parser = new JSON();
var obj = parser.decode(str);
	
var name = obj.name;
var term = obj.term_date.toString();

//print results
template.print(term + '.</p><p>' + name + '</p>');