Trigger an email based on two conditions

Diorella Angulo
Tera Expert

I have the following code which obviously does not work. The idea is to send an email using Event is fired.

I want to include in the email the part numbers where the Ready for UDI Data Entry is "Yes" and the Complete Status is "In Progress".

Each record is associated to an SCTASK, so I need to send one email where the Task No's are the same so I can include the part numbers for that Task No.

In this example when I change the Ready for UDI Data Entry to Yes, it should trigger an email notification for SCTASK0080343 that includes its respective Part Numbers, same case for SCTASK0080220 but that should be a separate email.

find_real_file.png

 

This is the scheduled script I have. Any help on this please?

var udi_gr = new GlideRecord('u_udi_tracking_data');
var gr = new GlideRecord('sc_task');
//gr.addQuery('state', 'Open'); //Check if the state is open
udi_gr.addEncodedQuery('u_status=Yes^u_complete_status!=Complete');
udi_gr.addQuery('udi_gr.u_task', 'gr.getUniqueValue()');
udi_gr.query();
while(udi_gr.next()) {	
	gs.eventQueue('UDI.EmailSME', udi_gr); //It will fire the event	
}
10 REPLIES 10

Hi Janos,

I tried your solution and it worked fine from what I need. The only thing is that I don't know how to pass the part numbers updated to the email notification using the event fire. In your case, let's say the 3 uniques values or the 3 short descriptions updated, show them in the email notification.

I was thinking on creating an Email Script, but I have not tried that yet. 

This is what I'm getting in the email notification (see the attachment)

If you have any idea or something please let me know. Thank you so much!

 

 

You have to use e-mail scripts for that. Assuming you transmit the list of sys_ids in parameter 1 to the event, you will have initialize a GlideRecord in that e-mail script, use the list in parameter 1 to filter it to the desired records, loop through those and probably print the desired list while doing that.

Something like:

(function runMailScript (current, template, email, email_action, event) {
	var gr = new GlideRecord('sc_task');
	var printTableRow = printTableRowForSchemeAndHost('https://' + gs.getProperty('instance_name') + '.service-now.com/');

	gr.addQuery('sys_id', 'IN', '' + event.parm1);

	gr._query();

	if (gr._next())
		printTable(gr);

	function printTable (gr) {
		template.print('<table>');

		printTableHeader(gr);
		printTableRow(gr);

		while (gr._next())
			printTableRow(gr);

		template.print('</table>');
	}

	function printTableHeader (gr) {
		var headerTemplate = '<tr><th>{0}</th><th>{1}</th></tr>';

		template.print(gs.getMessage(headerTemplate, [gr.number.getED().getLabel(),
													  gr.short_description.getED().getLabel()]));

	}

	function printTableRowForSchemeAndHost (schemeAndHost) {
		var rowTemplate = '<tr><td><a href="{0}" target="_blank">{1}</a></td><td>{2}</td></tr>';

		return function (gr) {
			template.print(gs.getMessage(rowTemplate, [schemeAndHost + gr.getLink(false),
													   GlideStringUtil.escapeHTML('' + gr.number),
													   GlideStringUtil.escapeHTML('' + gr.short_description)]));
		};
	}
})(current, template, email, email_action, event);

Assuming you want to print Catalog Tasks and Number (as anchor) and Short description thereof.

Hi,

I was not able to make the notification works, I worked at some point but now it is not working. I'm not sure if something changed.

Please see my attachments for "onAfter Update Status" Business Rule.

And I don't know what value pass as parameter. 

The InfoMessage works fine.

Please let me know if you have any idea.

Sorry for the late response but if you could help me would be great.

I will try to make the notification works via Event is fired.

 

There are some problems with the BR code:

1. Only the gs.addInfoMessage statement is executed once. The raising of the event will be executed always as it is not "under" the if's condition. You need to enclose both statements into curly braces:

if ((U_UPDATED_COUNT - U_UPDATED_COMMITTED - U_UPDATED_ABORTED) == 0) {
	gs.addInfoMessage...
	gs.eventQueue...
}

2. The 2nd parameter for the gs.eventQueue call must be a valid GlideRecord. In your code it is a GlideElement (current.u_new_finished_good_upn_part_number). If u_new_finished_good_upn_part_number is a reference, the 2nd parameter could be current.u_new_finished_good_upn_part_number.getRefRecord(). If that field is not a reference, you need to create and load a new record into a GlideRecord and use that as 2nd parameter - then needs to be a record in the same table as defined in the event and in the notification.

3. Unless UDI.EmailSME is a string containing the event name, it will not work.

Please do post code (too) not pictures of code - when code is involved.

Hi, thanks for your reply. 

I was able to find a different way to trigger the event. I used a scheduled script and pass the list as an array then use the mail script to built the table with the list. 

Thanks for sending the code for mail script it helped me to built the table.