Need to trigger 2 events in On-complete Transform Map scripts

Mohammed Hyder2
Kilo Expert

Hi All,

I need to trigger email notification based on the event triggered, If we have errors in transform history then i have to trigger 'xyz' event , if if there are no errors then 'abc' event..

 

1 ACCEPTED SOLUTION

Hi,

So you created after update BR as per above screenshots;

please update code as below; I forgot to query Import Set table with the current Import Set and then passed the GlideRecord Object to that eventQueue()

Updated Now: Ensure you set valid recipient for the event;

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

	// Add your code here

var importSet = new GlideRecord('sys_import_set');
importSet.addQuery('sys_id', current.set);
importSet.query();
if(importSet.next()){

	if(current.state == 'complete' && current.total > 0){
		gs.eventQueue("sn_customerservice.branch.alldata", importSet, recipient, importSet.number);
	}
	else if(current.state == 'complete_with_errors' || current.total == 0){
		gs.eventQueue("sn_customerservice.branch.info.failure", importSet, recipient, importSet.number);
	}

}

})(current, previous);

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

17 REPLIES 17

Pratiksha Kalam
Kilo Sage

Hello,

If I understand correctly, you want to trigger a mail when all the rows have been transformed with details for all the failed rows.

1 ) Define a variable in the onStart script for the map. ((Do not encapsulate it in a function, keep it outside all functions so that other scripts can access the same).

2 ) Update the content of this variable each time you find any error in your onBefore script.

3 ) Use the same variable to pass the accumulated error data to the email notification in onComplete transform script, using gs.eventQueue() function.

https://community.servicenow.com/community?id=community_question&sys_id=5dd54b21db1cdbc01dcaf3231f96...

If answer is helpful mark correct!

Thanks,

Pratiksha

asifnoor
Kilo Patron

Hello Ali,

You can trigger an event under onComplete transform script. In this script you can do glide record like this and check

var gr = new GlideRecord("sys_import_set_row_error");
gr.addEncodedQuery("row.sys_import_set="+source.import_set+"^sys_created_onONLast 15 minutes@javascript:gs.beginningOfLast15Minutes()@javascript:gs.endOfLast15Minutes()");
gr.query();
if(gr.next()) {
//there are errors. so trigger your xyz event here
gs.eventQueue("your_xyx",gr,gs.getUserName(),gs.getUserID());
} else {
// no errors.
gs.eventQueue("other_event",gr,gs.getUserName(),gs.getUserID());
}

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

Hi Asif, i need to consider Transform History where Total is also '0' , to notify that there is an error in the notification

 

 

In that case, try like this

var gr = new GlideRecord("sys_import_set_row_error");
gr.addEncodedQuery("row.sys_import_set="+source.import_set+"^sys_created_onONLast 15 minutes@javascript:gs.beginningOfLast15Minutes()@javascript:gs.endOfLast15Minutes()");
gr.query();
if(gr.next()) {
  //there are errors. so trigger your xyz event here
  gs.eventQueue("your_xyx",gr,gs.getUserName(),gs.getUserID());
} else {
  // check he total
  var gr = new GlideRecord("sys_import_set_run");
  if(gr.get(source.import_set)) {
    if(gr.total ==0) {
      //there are errors. so trigger your xyz event here
      gs.eventQueue("your_xyx",gr,gs.getUserName(),gs.getUserID());
    } else {
      gs.eventQueue("other_event",gr,gs.getUserName(),gs.getUserID());
    }
  } 
}

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP