Model Name mapping
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 09:48 AM
Hi All,
I am creating transform map for the import to Hardware Asset table. I have one requirement .If imported model name matches with service now model name then create asset with same model and if imported model name does not match with service now model name then check for the model which contains source manufacturer ,source Model number ,source model category ,Then create asset with that model and if there is no model exist then in this case row should be ignored and Asset should not be created.
Please help.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 09:55 AM
Hi @_bhishek ,
Can u try this script,create it as a onBefore one
(function runTransformMap(source, map, log, target, targetECC) {
// Get imported model name from the source data
var importedModelName = source.u_model_name; // Replace with the actual field name from your import set table
// Check if imported model name matches any existing model in ServiceNow
var model = new GlideRecord('cmdb_model');
model.addQuery('name', importedModelName);
model.query();
if (model.next()) {
// Model with imported name exists, use this model
target.cmdb_model = model.sys_id;
} else {
// Model with imported name not found, try finding a model based on manufacturer, model number, and category
var manufacturer = source.u_manufacturer;
var modelNumber = source.u_model_number;
var category = source.u_model_category;
model = new GlideRecord('cmdb_model');
model.addQuery('manufacturer', manufacturer);
model.addQuery('model_number', modelNumber);
model.addQuery('category', category);
model.query();
if (model.next()) {
// Found a matching model, use this model
target.cmdb_model = model.sys_id;
} else {
// No matching model found, ignore this record
log.info('Row ignored: No matching model found for ' + importedModelName);
return;
}
}
// Set other target fields from source data as needed
target.serial_number = source.u_serial_number; // Replace with the actual field name for serial number
// Map other fields from source to target as needed
})
(source, map, log, target, targetECC);
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 10:32 AM
Hi @_bhishek ,
Hope you are doing great.
- Check if the imported model name matches any model in ServiceNow.
- If it doesn’t match, it will then look for a model that contains the source manufacturer, source model number, and source model category.
- If no matching model is found, the row will be ignored, and no asset will be created.
Sample reference script for transform map:
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
var modelName = source.u_model_name.toString(); // Assuming u_model_name is your source field
var manufacturer = source.u_manufacturer.toString();
var modelNumber = source.u_model_number.toString();
var modelCategory = source.u_model_category.toString();
// Query the cmdb_model table to find a matching model
var modelGR = new GlideRecord('cmdb_model');
modelGR.addQuery('name', modelName);
modelGR.query();
if (modelGR.next()) {
// If model name matches, set the model in the target asset
target.model = modelGR.sys_id;
} else {
// If model name doesn't match, look for model with matching manufacturer, model number and category
modelGR = new GlideRecord('cmdb_model');
modelGR.addQuery('manufacturer', manufacturer);
modelGR.addQuery('model_number', modelNumber);
modelGR.addQuery('category', modelCategory);
modelGR.query();
if (modelGR.next()) {
// If a matching model is found, set the model in the target asset
target.model = modelGR.sys_id;
} else {
// If no matching model is found, ignore the row
ignore = true;
}
}
})(source, map, log, target);
Regards,
Riya Verma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2023 11:08 AM
Hi @_bhishek ,
If our responses where helpful to you could u please mark the responses as solution accepted as it will help other users to find the right answer
Thanks,
Danish