Unable to set the value for Reference field in Target table - Transform script

Mrman
Tera Guru

Hi All,

I am working on a Transform script to populate a reference field called model_id in the Target table(cmdb_ci).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.

As per logs I am able to see that I am getting the value , however it is not assigning to model_id in target field . It still shows as Unknown. Please see below what I found in logs and please guide.

find_real_file.png

Code:

====

(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'){

  var pm = new GlideRecord('pc_vendor_cat_item');

  pm.addQuery('short_description',res);

  pm.query();

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

  if(pm.next()){

  gs.log("into secondif"); ---- I am getting in here and able to see this logs.

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

  gs.log("ModelValue: "+mdel); ---- I am able to see this logs and coming correctly as ModelValue: HP ProLiant DL380 G7

  target.model_id = mdel;

  gs.log("Target :"+target.model_id); --- I am able to see the value correctly here also in logs as Target :HP ProLiant DL380 G7

      }

1 ACCEPTED SOLUTION

Gurpreet07
Mega Sage
7 REPLIES 7

Gurpreet07
Mega Sage

Here you need to use


target.model_id.setDisplayValue(mdel);


Refer below link


http://wiki.servicenow.com/index.php?title=Inserting/Updating_GlideRecord_with_References#gsc.tab=0


Hi Gurpreet,



This did not work.Please guide.



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(mdl!=''){




  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();


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



  if(pm.next()){



  gs.log("into secondif");



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


  gs.log("ModelValue: "+mdel);


 



  target.model_id.setDisplayValue(mdel);


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


  //}




  }


Gurpreet07
Mega Sage

One similar thread of yours


Return sysid to a reference field


michael_baker
Tera Guru

Hi Ravi,



I believe you want to set "target.model_id" to the sys_id of the pc_vendor_cat_item record, like below:



target.model_id = pm.sys_id;



Hope this helps!