- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2022 09:29 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2022 10:42 AM
Hi
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
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2022 09:42 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2022 09:50 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2022 10:24 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2022 10:42 AM
Hi
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
Regards,
Shloke