Returning multiple values through glide record then using them in the next sub loop

anfield
Tera Guru

Hello. I was hoping I could get some code advice. I have a temp table (a snippet in the attached image) Basically what I am doing is where I have duplicate models in the model table I pull into this table and prepare for to update the CI and asset on the target tables with the target sys id from the temp table. I set the CI update field to yes and then run the script. I have tested on a few models so far, and works with only one model set at a time

 

But the issue is:  If I run this on say multiple model sets, like inspiron 00015 and Inspiron 3847 in the image then it gets confused and puts the data all into one CI and asset.

 

I think the issue might be that I need to return 2 values from my first glide record query, the name and also the target id. I think maybe it is just getting one target model sysid, and update all the CI's and assets with that id.

How can I do that? I need to then reference those two values in the for loop that does the push.

The code sample is only part of the code, the CI function is basically the same.  Thanksfind_real_file.png

 

function update_asset_models(){
	
		// Step 5 - find the assets with the model sysids, and update with the target sysids
	
		model_ids = [];

		var grm1 = new GlideRecord('u_temp_table');
		
		grm1.addQuery('u_model_sysid', '!=' ,'');
		grm1.addQuery('u_ci_update', '=', 'yes');
		grm1.autoSysFields(false);
		grm1.setWorkflow(false);
		grm1.query();
	
		while (grm1.next()){
			
			var model_sysid = grm1.getValue('u_model_sysid');
			gs.log('Product Model: sysid: ' + model_sysid);
			
			var target_modelsysid = grm1.u_target_model_sysid;
			gs.log('Product Model: Targetsysid: ' + target_modelsysid);
			
			model_ids.push(model_sysid);
			
			} 
	
		gs.log('Product Model: Arr: len: ' + model_ids.length);


	for (var i=0; i < model_ids.length; i++) {	
		
		
			var grHasAsset = new GlideRecord('alm_hardware');
		
				grHasAsset.addEncodedQuery('model.sys_id=' + model_ids[i]);
				grHasAsset.autoSysFields(false);
				grHasAsset.setWorkflow(false);
				grHasAsset.setValue('model', target_modelsysid);
				grHasAsset.updateMultiple();
		
				gs.log('Product Model: Asset Targetsysid: ' + target_modelsysid);
		
				gs.log('Product Model: Asset Found: ' + model_ids[i]);	
				gs.log('Product Model: Asset Row Count: ' + grHasAsset.getRowCount());
				
			}
	
} update_asset_models();

 

 

3 REPLIES 3

DirkRedeker
Mega Sage

Hi

Try this (I did not run the code, so check please for potential syntax errors):

function update_asset_models(){
	
		// Step 5 - find the assets with the model sysids, and update with the target sysids
	
		var model_ids = [];
                var target_ids = [];

		var grm1 = new GlideRecord('u_temp_table');
		
		grm1.addQuery('u_model_sysid', '!=' ,'');
		grm1.addQuery('u_ci_update', '=', 'yes');
		grm1.autoSysFields(false);
		grm1.setWorkflow(false);
		grm1.query();
	
		while (grm1.next()){
			
			var model_sysid = grm1.getValue('u_model_sysid');
			gs.log('Product Model: sysid: ' + model_sysid);

			var target_modelsysid = grm1.u_target_model_sysid;
			gs.log('Product Model: Targetsysid: ' + target_modelsysid);
			
			model_ids.push(model_sysid);
                        target_ids.push(target_modelsysid);

			
			} 
	
		gs.log('Product Model: Arr: len: ' + model_ids.length);


	for (var i=0; i < model_ids.length; i++) {	
		
		
			var grHasAsset = new GlideRecord('alm_hardware');
		
				grHasAsset.addEncodedQuery('model.sys_id=' + model_ids[i]);
				grHasAsset.autoSysFields(false);
				grHasAsset.setWorkflow(false);
//				grHasAsset.setValue('model', target_modelsysid);
                                grHasAsset.setValue('model', target_ids[i]);
				grHasAsset.updateMultiple();
		
				gs.log('Product Model: Asset Targetsysid: ' + target_modelsysid);
		
				gs.log('Product Model: Asset Found: ' + model_ids[i]);	
				gs.log('Product Model: Asset Row Count: ' + grHasAsset.getRowCount());
				
			}
	
} update_asset_models();

Note:

I added a new array "target_ids[]" which I loaded in the first loop and use in the second loop.

Check out if that works and let me know and mark my answer as correct and helpful.

Have fun coding.

BR

Dirk

Hi Dirk. I will try this in the future. I ended up getting rid of the for loop, and just going into a subloop so I could access the variables. Thanks for your input

Hi Thanks for the update. Just remember me and get back here, as soon as you found out. Let me know BR Dirk