Model Name mapping

_bhishek
Tera Guru

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 

3 REPLIES 3

Danish Bhairag2
Tera Sage
Tera Sage

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

 

Riya Verma
Kilo Sage
Kilo Sage

Hi @_bhishek ,

 

Hope you are doing great.

 

  1. Check if the imported model name matches any model in ServiceNow.
  2. If it doesn’t match, it will then look for a model that contains the source manufacturer, source model number, and source model category.
  3. 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);
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

Danish Bhairag2
Tera Sage
Tera Sage

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