Remove Multiple Duplicate Model Script help

runfast
Kilo Guru

I am trying to enhance the SN remove duplicate model script, which manages single duplicate records, to be able to process multiple duplicate models.

Here is the SN remove duplicate model which manages single record. 

 

//Move CI from the bad dup model name to the good dup model
var xcorrect_model = 'sysid1'; //The desired dup model SYS ID where the CI's will be moved from xincorrect_model
var xincorrect_model = 'sysid2';//The desired dup model SYS ID where the CI's will be moved to the xcorrect_model sys id.

var xreloc_assets= [ {"table":"alm_asset","field":"model"},{"table":"cmdb_m2m_model_component","field":"parent"},{"table":"pc_vendor_cat_item","field":"model"},{"table":"cmn_m2m_skill_model","field":"model"} ];

for(var xitem=0;xitem < xreloc_assets.length; xitem++)
   relocate(xreloc_assets[xitem].table,xreloc_assets[xitem].field,xcorrect_model,xincorrect_model);

function relocate(p_table,p_field,p_correct_model,p_incorrect_model) {

    var xresult = false;
    try {
        var xgr = new GlideRecord(p_table);
        xgr.addQuery(p_field,p_incorrect_model);
        xgr.setWorkflow(false); //Avoiding any further processing related to the update
        xgr.setAutoSysFields(false); //Avoid update timestamp modification
        xgr.query();
        if(xgr.getRowCount() > 0) {
            gs.info('Relocating ' + p_table );
            xgr.setValue(p_field,p_correct_model);
            xgr.updateMultiple();
            xresult = true;
        } else {
            gs.info('No Data for ' + p_table );
        }

    } catch(e) {
      gs.info(e.message);
      gs.info(e.stack);
    }

    return xresult;

}

 

I created an array with a list of targeted sysids  which will be passed on to the "Relocate" function.

 

var array =  ["0bc35b9b1bf805d0e40acbbf03445125|0b1200921b617c10e304c91862478428"];

 

Then I created a loop to go through the array:

 

for (i =0; i < array.length; i++) {
	var getSysIDs = array[i];
	gs.info('sysIds' + getSysIDs);
	var x = getSysIDs.split("|");
	var recentDupModSysID = x[0];
	var oldDupModSysID = x[1];

 

 

 

 

I get the correct value when I do gs.info(recentDupModSysID + ":" + oldDupModSysID);

The challenge I am having is when I try to put this together, I am getting "Relocate is not a function"

Here is the complete work in progress script:

 

var array =  ["0bc35b9b1bf805d0e40acbbf034b4561|0b1200921b617c10e304c918624b8523"];
    
//Move CI from the bad dup model name to the good dup model

for (i =0; i < array.length; i++) {
	var getSysIDs = array[i];
	gs.info('sysIds' + getSysIDs);
	var x = getSysIDs.split("|");
	var recentDupModSysID = x[0];
	var oldDupModSysID = x[1];
	
//Move CI from the bad dup model name to the good dup model
var xcorrect_model = recentDupModSysID;
var xincorrect_model = oldDupModSysID;

var xreloc_assets= [ {"table":"alm_asset","field":"model"},{"table":"cmdb_m2m_model_component","field":"parent"},{"table":"pc_vendor_cat_item","field":"model"},{"table":"cmn_m2m_skill_model","field":"model"} ];

for(var xitem=0;xitem < xreloc_assets.length; xitem++)
   relocate(xreloc_assets[xitem].table,xreloc_assets[xitem].field,xcorrect_model,xincorrect_model);

function relocate(p_table,p_field,p_correct_model,p_incorrect_model) {

    var xresult = false;
    try {
        var xgr = new GlideRecord(p_table);
        xgr.addQuery(p_field,p_incorrect_model);
        xgr.setWorkflow(false); //Avoiding any further processing related to the update
        xgr.setAutoSysFields(false); //Avoid update timestamp modification
        xgr.query();
        if(xgr.getRowCount() > 0) {
            gs.info('Relocating ' + p_table );
            xgr.setValue(p_field,p_correct_model);
            xgr.updateMultiple();
            xresult = true;
        } else {
            gs.info('No Data for ' + p_table );
        }

    } catch(e) {
      gs.info(e.message);
      gs.info(e.stack);
    }

    return xresult;

}}
			
		
	

 

 

 

 any direction or guidance is greatly appreciated.

Here is the error I am getting:

 

 

 

 

Evaluator: com.glide.script.RhinoEcmaError: relocate is not a function.
   script : Line(19) column(0)
     16: var xreloc_assets= [ {"table":"alm_asset","field":"model"},{"table":"cmdb_m2m_model_component","field":"parent"},{"table":"pc_vendor_cat_item","field":"model"},{"table":"cmn_m2m_skill_model","field":"model"} ];
     17: 
     18: for(var xitem=0;xitem < xreloc_assets.length; xitem++)
==>  19:    relocate(xreloc_assets[xitem].table,xreloc_assets[xitem].field,xcorrect_model,xincorrect_model);
     20: 
     21: function relocate(p_table,p_field,p_correct_model,p_incorrect_model) {
     22: 

 

 

 

 

1 ACCEPTED SOLUTION

runfast
Kilo Guru

I had the close curly bracket for the for loop in the wrong place. 

I put the close curly bracket right before the relocate function to get it to work.

View solution in original post

1 REPLY 1

runfast
Kilo Guru

I had the close curly bracket for the for loop in the wrong place. 

I put the close curly bracket right before the relocate function to get it to work.