- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2024 07:56 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2024 08:17 AM - edited 09-10-2024 08:18 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2024 09:45 AM
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2024 08:17 AM - edited 09-10-2024 08:18 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2024 09:31 AM
@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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2024 09:45 AM
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2024 09:51 AM
Thank you again, I was over complicating it!