Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Transform map Scripts

Vignesh21
Kilo Guru

Hi,

Can someone let me know why its importing record from excel.

Requirement: Ignore a record to be inserted if u_name field on source records is empty.

I have created onBefore scripts as below

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

	
	if(source.u_name == '' && action == 'insert'){
	
	ignore = true;
	
}
})(source, map, log, target);

 

also i have onComplete script

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

	var gr = new GlideRecord ('cmdb_ci_ec2_instance');
	gr.addQuery('state','!=','terminated'); 
	gr.query();
	while(gr.next()){
			
		
		var src = new GlideRecord('ec2__load');
		src.addQuery('u_instance_id',gr.object_id);
		src.addQuery('sys_import_set.number',import_set.number);
		src.query();
		if(!src.next()){
			
 			gr.state = 'terminated';
 			gr.update();
		}
	
	}
	
	var gr1 = new GlideRecord ('cmdb_ci_ec2_instance');
	gr1.addNullQuery('u_i_project'); 
	gr1.addQuery('state','!=','terminated');
	gr1.addQuery('state','!=','terminating');
	gr1.query();
	while(gr1.next()){
		gr1.state = 'error';
		gr1.update();
	}
	})(source, map, log, target);

Another script is onAfter :

if(source.u_cost_center == ""  ){
		target.state = 'error';
		target.cost_center = "";
		target.update();
	}

Once I load the data without u_name it's not inserting records, but if any other column u_i_project & u_cost_center has empty value then its inserting a recordsbut empty value as shown below. 

find_real_file.png

 

Can someone help me why i am getting these records inserted?

 

Thanks,

Vignesh

10 REPLIES 10

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

I would check the values in the onBefore to make sure you get what you expect. So something like this in the onBefore script:

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

	gs.info("Test u_name length: " + source.u_name.length + " and action: " +action);
	if(source.u_name == '' && action == 'insert'){
	
	ignore = true;
	
}
})(source, map, log, target);

What do you get from this?

//Göran
Feel free to connect:
LinkedIn
Subscribe to my YouTube Channel
or look at my Book: The Witch Doctor’s Guide To ServiceNow

Thanks Goran for the reply,

 

I am getting this in logs

try do the code like this:

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

	
	if(source.u_name.nil() && action == 'insert'){
	
	ignore = true;
	
}
})(source, map, log, target);

Goran still its creating new record after this script

Can you try. JSUtil.nil(source.u_name)
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022