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

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.

@Brad Bowman , what if I want to run the business rule on the asset table? This way when a new asset is inserted or updated it will pull the Chassis type from the model, that one works on the model table, but I want the chassis pulled from that table to the asset whenever an asset is created. I tried to switch it around but it's not working properly. How would you write it if you're running the rule from the alm_asset table? What syntax do you use to trigger the update since I am running it on the asset table I wouldn't use the gr.update correct? Would this scenario be different, or you still only use the one gliderecord since my rule is on the asset table?

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;

 

Thank you again, I was over complicating it!