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

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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Ali,

you can use sample script below in onComplete transform script

1) query Transform History table with the current import set

2) check if state is Complete or not

a) if complete then no error

b) if not complete then some error

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

var gr = new GlideRecord('sys_import_set_run');
gr.addQuery('set', source.sys_import_set);
gr.query();

if(gr.next()){

if(gr.state == 'complete'){
// no error send email 1
gs.eventQueue('event_no_error', gr, 'recipient','');
}
else{
// error so send email 2
gs.eventQueue('event_error', gr, 'recipient','');
}

}

})(source, map, log, target);

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

MrMuhammad
Giga Sage

Hi,

You can user below script to trigger notifications based off the condition that the transform history contains error or not for the current import set.

var gr = new GlideRecord("sys_import_set_run"); // GlideRecord transform history table and find if any errors occured.
gr.addEncodedQuery("set="+source.import_set+"^errors!=0");
gr.query();
if(gr.next()) {
//there are errors. so trigger your first event here
gs.eventQueue("event_name",gr,gs.getUserName(),gs.getUserID());
} else {
// no errors. trigger other event here.
gs.eventQueue("second_event_name",gr,gs.getUserName(),gs.getUserID());
}

 

Please mark this accepted/helpful, if applicable.

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad