The CreatorCon Call for Content is officially open! Get started here.

onComplete script is help

Jyoti Tripathi
Giga Guru

Hi All,

 

I was creating a onComplete transform script to map the location and code in the HR case if the order number is matched from the excel sheet.

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

	 var loc = source.u_location;
    var jobCode = source.u_job_code;
    var orderNum = source.u_order_id;
	
	var orMap = new GlideRecord('sn_hr_core_case');
	orMap.addEncodedQuery('active=true^u_order_numberISNOTEMPTY');
	orMap.addQuery("u_order_number",orderNum);
  orMap.query();
	while(orMap.next()){
		orMap.u_location= loc;
		orMap.u_job_code= jobCode;
		orMap.update();
	}
})(source, map, log, target);


But the onComplete script is not working, please help

5 REPLIES 5

Nitesh A
Tera Expert

Hello @Jyoti Tripathi 

 

I'm afraid that this onComplete transform script won't work.

Instead create a field map and add the script there, so for every record it will check for the orderNum and if found it will update the location and jobcode accordingly.

 

Please mark this response as correct or helpful if it assisted you with your question.

Regards,
Nitesh

@Nitesh A : Could you please help me how to write the script in field mapping?

JyotiTripathi_0-1690531232126.png

 

Weird
Mega Sage

How is it not working? Have you tried logging?
Using onComplete means that it's run at the end after each row has been processed. Using source.field_name there is using the last processed row as reference, so if you've meant to run this for each transform then you need to change it to onAfter.


But your code itself seems OK. Maybe consider combining the encoded query and query to make sure that's not causing issues (it shouldn't, but you can try).

 

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

	var loc = source.u_location;
	var jobCode = source.u_job_code;
	var orderNum = source.u_order_id;

	if(orderNum != ''){
		var orMap = new GlideRecord('sn_hr_core_case');
		orMap.addEncodedQuery('active=true^u_order_number=' + orderNum);
		orMap.query();
		while(orMap.next()){
			orMap.u_location = loc;
			orMap.u_job_code = jobCode;
			orMap.update();
		}
	}
})(source, map, log, target);

 


But to make sure it's running OK, I'd recommend adding logging and checking syslog table to see what it is doing .

For example this would tell you a lot about what is going on:

 

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

	var loc = source.u_location;
	var jobCode = source.u_job_code;
	var orderNum = source.u_order_id;
gs.info("Transform run for orderNum: " + orderNum);
	if(orderNum != ''){
		var orMap = new GlideRecord('sn_hr_core_case');
		orMap.addEncodedQuery('active=true^u_order_number=' + orderNum);
		orMap.query();
		while(orMap.next()){
gs.info("Transform run on case: " + orMap.number + "with values for loc and job:" + loc + " and " + jobCode); //Assuming cases have numbers
			orMap.u_location = loc;
			orMap.u_job_code = jobCode;
			orMap.update();
		}
	}
})(source, map, log, target);

 

 

In the logs you can search for messages starting with "Transform run" and then you'll see which orderNum and case it ran on and if looking at errors you could even see if there was an error running the script.

Hi @Weird : Before my code was written on OnAfter condition, but the data is huge and took around 2 hours to complete the transform mapping. SO was thinking to do it with onComplete.