Populating String value to a Reference field in Target Table.

Mrman
Tera Guru

Hi All,

Can some one please help me with the below Transform Script. Please let me know if this fine. I want to Match the Concatenated string from my Custom table into a field called model_id into cmdb_ci which is reference field.

Script:

=====

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

  var mnfc = source.u_manufacturer.trim();

  var mnfcr = mnfc.replace(/,/g, "");

  var mdl = source.u_model.trim();

  //var res = mnfcr.concat(mdl);

  var res = mnfcr + " " + mdl;

  gs.log("Concatenated string : "+res);

  var type = source.u_ci_server_type;

   

  if(type!='Virtual'){

  //gs.log("into if");

  var pm = new GlideRecord('pc_vendor_cat_item');

  //pm.addQuery('model.display_name',res);

  pm.addQuery('short_description',res);

  pm.query();

  pm.getEncodedQuery("QueryResult :"+pm);

  if(pm.next()){

   

  target.model_id = pm;

  gs.log("Target :"+target.model_id);

  }

  else{

  ignore = true;

  // code for logging error messages

  gs.include('data_load_logs_asset');

  var pmdl = new data_load_logs_asset();

  var logmessage ='';

  var topic ='';

  var messagetype = '';

  topic = serial;

  logmessage =logmessage + "*-*" + "Warning: The Model "+res+" is not present in vendor catalog table.";

  messagetype = messagetype + "*-*" + "Warning";

  pmdl.insertLogMessage(tranform_map,logmessage,topic, import_number, count, messagetype);

  }

  }

})(source, map, log, target);

1 ACCEPTED SOLUTION

Hi Ravi,



This (below code) will try to assign the string field in model_id,



var mdel = pm.getValue('short_description');


target.model_id = mdel;



but since you told it's a reference field then in that case you have to assign sys_id of found record. Please try if this helps.



target.model_id = pm.sys_id;



Also please check what is the display value set for model_id field's table?


View solution in original post

9 REPLIES 9

ccajohnson
Kilo Sage

It is unclear what you are tying to accomplish.



It appears as if you are looking up a model name in your custom table in order to populate the Hardware Model in the CI record. Unsure if you have established Hardware Model records that correspond to the models in your custom table, or not.



Model records are the key to determining the type of CI that is created. If you have not created model records to use, then this import script will not work. I would recommend reading the documentation ServiceNow has for Models before you start importing records:


Models


Hi Christopher,



I am trying to Concatenate the Manufacturer and Model from my Custom Table and querying against the short_description in pc_vendor_cat_item with the Concatenated result



If record exists then assign the Short description to the model_id in cmdb_ci whoch is a refernec field.



Please help.



Regards,


Ravi G


In if condition, i think this is wrong : target.model_id = pm;



it should be : target.model_id = pm.short_description;


Hi,



Please let me know if this is fine.


(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {



  var mnfc = source.u_manufacturer.trim();


  var mnfcr = mnfc.replace(/,/g, "");


  var mdl = source.u_model.trim();


  var res = mnfcr + " " + mdl;


  var type = source.u_ci_server_type;


 


    if(type!='Virtual'){


  //gs.log("into if");




  var pm = new GlideRecord('pc_vendor_cat_item');


  pm.addQuery('short_description',res);


  pm.query();


   


  if(pm.next()){



  //gs.log("into secondif");



  var mdel = pm.getDisplayValue('short_description');


    if(mdel == res){


   


    target.model_id = mdel.short_description;


  gs.log("Target :"+target.model_id);


  }




  }



  else{




  ignore = true;


  // code for logging error messages


  gs.include('data_load_logs_asset');


  var pmdl = new data_load_logs_asset();


  var logmessage ='';


  var topic ='';


  var messagetype = '';



  topic = serial;


  logmessage =logmessage + "*-*" + "Warning: The Model "+res+" is not present in vendor catalog table.";


  messagetype = messagetype + "*-*" + "Warning";


  pmdl.insertLogMessage(tranform_map,logmessage,topic, import_number, count, messagetype);



  }





  }





  //}



  })(source, map, log, target);