The CreatorCon Call for Content is officially open! Get started here.

Syntax Help! Copy a field from model to asset with that same model

jonsr20
Tera Expert

Hey everyone,

 

I created a chassis Column on alm_asset table and cmdb_hardware_product_model table to help identify assets. Example laptops, desktops, network etc. I am trying to create a business rule to have this field pushed from the model to assets. I am struggling with the correct syntax; I am pretty new at scripting and any assistance would be appreciated! Here is what I have so far but have tried a variety. I am not sure if I can run 2 queries like this and my if statement is probably incorrect. Any thoughts?

 

var md = new GlideRecord('cmdb_hardware_product_model');
	var gr = new GlideRecord('alm_asset');
	md.addEncodedQuery('u_chassis_typeISNOTEMPTY');
	md.query();
	gr.addQuery('sys_id' , 'model');
	gr.query();
	if (md == gr.next()) {
	
		var mChassis = md.u_chassis_type;
		var aChassis = gr.u_chassis_type;
		gr.setWorkflow(false);
        gr.autoSysFields(false);
		aChassis = mChassis;
		gr.update();
		
	}	

Thanks,

Jon

2 ACCEPTED SOLUTIONS

Brad Bowman
Kilo Patron
Kilo Patron

You only need one GR since the BR should be running on the model table after update when chassis changes.  You have access to the entire record that is being updated by using current.field_name, so the script would be more like

 

var gr = new GlideRecord('alm_asset');
gr.addQuery('model', current.sys_id);
gr.query();
while (gr.next()) {
    gr.setWorkflow(false);
    gr.autoSysFields(false);
    gr.u_chassis_type = current.u_chassis_type;
    gr.update();
}

 

This uses the while loop in the GR so that every asset record which uses this model is updated.

View solution in original post

Do this before insert, and update if you want but be sure to add a Filter Condition for chassis is empty so that it doesn't run unnecessarily.  Since the model field is a reference on the asset table you can just dot-walk it, so your script is one line:

current.u_chassis_type = current.model.u_chassis_type;

 

View solution in original post

6 REPLIES 6

Community Alums
Not applicable

Hello @jonsr20 ,

 

The script updates the `u_chassis_type` field on `alm_asset` records by matching the `model` field with corresponding `u_chassis_type` from `cmdb_hardware_product_model`.

var md = new GlideRecord('cmdb_hardware_product_model');
md.addNotNullQuery('u_chassis_type'); 
md.query();

while (md.next()) {
    var gr = new GlideRecord('alm_asset');
    gr.addQuery('model', md.sys_id); 
    gr.query();
    
    while (gr.next()) {
        gr.setWorkflow(false); 
        gr.autoSysFields(false); 
        gr.u_chassis_type = md.u_chassis_type;
        gr.update(); 
    }
}

 

If this helped you in any way, please hit the like button/mark it helpful. So it will help others to get the correct solution.

jonsr20
Tera Expert

Thank you both, you guys are the best!!!

 

Jon