Log while running runscript of transform map

Giri6
Tera Expert

I have runscript for transform map that is supposed to either create or update. I set ignore= true as  I wanted only runscript code to be executed. I feed 570 rows of data and see  442 created and 68 updated. Why there is delta (570 -510=60 rows) here. I don't have any data that has both serial number and asset tag empty.

How do I debug to print row number and show if it is creating or updating. Here is the code. Appreciate it.

 

find_real_file.png

1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi @Giri 

The way you have currently written your code is going to add lot of complexity while debugging this.

I would strongly recommend here the use small methods and update your code accordingly which will not only make it easier for anyone to read and understand but also allow you to debug things.

What I understood IF Serial and Asset tag is present in your Hardware asset table then you are updating it else you are creating new Record. If this assumption is correct then update your code as below:

I have re written the code for you in making it more structured and easy to understand. Please use the same below.

Also , can you tell me the scenario when you are Updating the record and when you are trying to create New record?

(function transformRow(source, target, map, log, isUpdate) {

        // Add your code here
        var gr = new GlideRecord('alm_hardware');
        gr.addQuery('serial_number', source.u_serial_number.toString());
        gr.query();
        if (gr.next()) {
            gr.install_status = 7;
            gr.update();
        } else if(JSUtil.nil(source.u_serial_number) && JSUtil.notNil(source.u_asset_tag)){
            checkAssetTag(source.u_asset_tag.toString());
        }else{
			createNewAssetRecord();
		}

        function checkAssetTag(assetTag) {
            var gr = new GlideRecord('alm_hardware');
            gr.addQuery('asset_tag',assetTag);
            gr.query();
            if (gr.next()) {
                gr.install_status = 7;
                gr.update();
            }
		}
	
	function createNewAssetRecord(){
		var gr = new GlideRecord('alm_hardware');
		gr.initialize();
		gr.model_category = getModelCategory(source.u_model_category.toString());
		gr.model = getModel(source.u_manufacturer,source.u_model);
		gr.serial_number = source.u_serial_number;
		gr.insert();
	}
	
	function getModelCategory(modelCategory){
		var gr = new GlideRecord('cmdb_model_category');
		gr.addQuery('name',modelCategory);
		gr.query();
		if(gr.next()){
			return gr.sys_id.toString();
		}
		
	}
	
	function getModel(manufacturer,model){
		var gr = new GlideRecord('cmdb_model');
		gr.addQuery('model_number',model);
		gr.query();
		if(gr.next()){
			return gr.sys_id.toString();
		}else{
			var mod = createNewModel(manufacturer,model);
			return mod.toString();
		}
	}
	
	function createNewModel(manufacturer,model){
		var gr = new GlideRecord('cmdb_model');
		gr.initialize();
		gr.model_number = model;
		gr.manufacturer = manufacturer;
		var success = gr.insert();
		if(success){
			return gr.sys_id.toString();
		}
	}

        })(source, target, map, log, action === "update");

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

5 REPLIES 5

Saurav11
Kilo Patron
Kilo Patron

Hello,

Do you not see those 60 as ignored/skipped/error in the Import set?

find_real_file.png

Please mark answer correct/helpful based on Impact

I see all ignored as  everything done through code.

find_real_file.png

Hello,

Easy way:-

Not sure how much freedom you have in the project but if you can create a string field

Just add created/update based on the action in the field so you can know if the field is empty then those records were nether created nor updated.

Hard way:-

You can just add gs.log('Update',+gr_hardware.serial_number) for Serial number loop

Then just add gs.log('Update',+gr_hardware.asset_tag) for asset tag loop

 

Then just add gs.log('Create',+gr_hardware.asset_tag) for create loop

 

This way you will get all the records that were created and updated in the logs

 

Also you can do it via excel as well just get the excel exported for all records which were updated today. then you can compare with the source excel which records were  missing.

Pease mark answer correct/helpful based on Impact

 

 

shloke04
Kilo Patron

Hi @Giri 

The way you have currently written your code is going to add lot of complexity while debugging this.

I would strongly recommend here the use small methods and update your code accordingly which will not only make it easier for anyone to read and understand but also allow you to debug things.

What I understood IF Serial and Asset tag is present in your Hardware asset table then you are updating it else you are creating new Record. If this assumption is correct then update your code as below:

I have re written the code for you in making it more structured and easy to understand. Please use the same below.

Also , can you tell me the scenario when you are Updating the record and when you are trying to create New record?

(function transformRow(source, target, map, log, isUpdate) {

        // Add your code here
        var gr = new GlideRecord('alm_hardware');
        gr.addQuery('serial_number', source.u_serial_number.toString());
        gr.query();
        if (gr.next()) {
            gr.install_status = 7;
            gr.update();
        } else if(JSUtil.nil(source.u_serial_number) && JSUtil.notNil(source.u_asset_tag)){
            checkAssetTag(source.u_asset_tag.toString());
        }else{
			createNewAssetRecord();
		}

        function checkAssetTag(assetTag) {
            var gr = new GlideRecord('alm_hardware');
            gr.addQuery('asset_tag',assetTag);
            gr.query();
            if (gr.next()) {
                gr.install_status = 7;
                gr.update();
            }
		}
	
	function createNewAssetRecord(){
		var gr = new GlideRecord('alm_hardware');
		gr.initialize();
		gr.model_category = getModelCategory(source.u_model_category.toString());
		gr.model = getModel(source.u_manufacturer,source.u_model);
		gr.serial_number = source.u_serial_number;
		gr.insert();
	}
	
	function getModelCategory(modelCategory){
		var gr = new GlideRecord('cmdb_model_category');
		gr.addQuery('name',modelCategory);
		gr.query();
		if(gr.next()){
			return gr.sys_id.toString();
		}
		
	}
	
	function getModel(manufacturer,model){
		var gr = new GlideRecord('cmdb_model');
		gr.addQuery('model_number',model);
		gr.query();
		if(gr.next()){
			return gr.sys_id.toString();
		}else{
			var mod = createNewModel(manufacturer,model);
			return mod.toString();
		}
	}
	
	function createNewModel(manufacturer,model){
		var gr = new GlideRecord('cmdb_model');
		gr.initialize();
		gr.model_number = model;
		gr.manufacturer = manufacturer;
		var success = gr.insert();
		if(success){
			return gr.sys_id.toString();
		}
	}

        })(source, target, map, log, action === "update");

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke