Asset Model,Model category field updates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2024 11:25 AM
Hi All,
I have 3 columns.Asset sysID,Model sysID,Model Category name in spreadsheet. I want to fix Model and Model category for each asset available in spreadsheet based on below condition.
1.If imported model sys ID is different than the current model for the asset then update the model and update the model only if new correct model is hardware model else no update on model field. If current model is same as imported model then no update on model field.
2.if imported model category is different than the current model category then update model category field .Also that model category should be available in imported model .
I have written some part of script for this requirement as onbefore script for transform map on asset table ,This part is working fine. Can you please help me with remaining part -
Model should be updated only if imported model is hardware model.
Model category should be updated only if respective imported model is having that model category.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2024 09:40 PM
HI @_bhishek ,
To complete your script, you need to add additional conditions to ensure that the updates are made only if certain criteria are met. Here's how you can modify your script to include these conditions:
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
var gr = new GlideRecord('alm_asset');
gr.addQuery('sys_id', source.u_asset_sysid);
gr.query();
while (gr.next()) {
if (source.u_model != gr.model) {
if (source.u_is_hardware_model == true) {
gr.model = source.u_model;
gr.update();
}
}
if (source.u_model_category != gr.model_category) {
var modelGr = new GlideRecord('model_table'); // Replace 'model_table' with the actual name of your model table
if (modelGr.get('sys_id', source.u_model_sysid) && modelGr.category == source.u_model_category) {
gr.model_category = source.u_model_category;
gr.update();
}
}
}
})();
If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.
Thanks,
Amitoj Wadhera
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2024 04:46 AM
Hi @_bhishek ,
You can try following logic to enhance your existing script:
Check if the imported model is a hardware model. and Ensure the model category of the imported model matches the imported model category.
(function runTransformScript(source, map, log, target ) {
var gr = new GlideRecord('alm_asset');
gr.addQuery('sys_id', source.u_asset_sysid);
gr.query();
while (gr.next()) {
var updateNeeded = false;
if (source.u_model != gr.model) {
var modelGR = new GlideRecord('cmdb_model');
if (modelGR.get(source.u_model) && modelGR.hardware_model) {
gr.model = source.u_model;
updateNeeded = true;
}
}
if (source.u_model_category != gr.model_category) {
if (modelGR.sys_id == source.u_model && modelGR.category == source.u_model_category) {
gr.model_category = source.u_model_category;
updateNeeded = true;
}
}
if (updateNeeded) {
gr.update();
} else {
ignore = true;
}
}
})(source, map, log, target);
If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.
Thanks,
Pooja Manchekar