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

Mike Patel
Tera Sage

try changing below on onbefore script

source.u_name.toString()

Thanks for replying Mike, 

Unfortunately it didnt help its still inserting a record. What other options do you think it might trigger this issue?

Can you try source.u_name.toString().trim(); Just to make sure source does not have empty strings.

 

Also what is the key you are matching on?

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Thanks, vkachineni !!

 Again no luck with this