Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Help Transform Field map script not working correctly

E_19
Giga Expert

Hi All,

I hope someone can help me.

I have a transform field map script that is not working correct. What I am trying to do is iterate though the source kunden number and for all matches that are found in core company table   the corresponding sys_id is added to u_company field but the script iterates and has the same company sys id for every record. In the log file it records same sys_id but also each unique kunden number.

Need to add the field is a reference field and the Referenced value field name: u_company

Any idea why it is not return the unique sys_id for each record?

answer = (function transformEntry(source) {

  var gr1 = new GlideRecord("core_company");

  gr1.addQuery("u_company",source.u_kundennr_.toString());

  gr1.query();

  if(gr1.next()){

  target.u_company = gr1.sys_id;

  }

  gs.log("Kundennummer: " + source.u_kundennr_);

  gs.log("sys_id von ?: " + gr1.sys_id);

  return ""; // return the value to be put into the target field

})(source);

Many Thanks,

Ellie

1 ACCEPTED SOLUTION

can you try like this


  var gr1 = new GlideRecord("core_company");


  gr1.addQuery("nr",source.u_kundennr_.toString());


  gr1.query();


  if(gr1.next()){


  target.u_company = gr1.sys_id;


// put your logs here


  }


View solution in original post

10 REPLIES 10

Dhruv Chandan
Giga Guru

Hi Ellie,



Try returning some value. Example



answer = (function transformEntry(source) {



  var gr1 = new GlideRecord("core_company");


  gr1.addQuery("u_company",source.u_kundennr_.toString());


  gr1.query();


  if(gr1.next()){


  target.u_company = gr1.sys_id;


  }


  gs.log("Kundennummer: " + source.u_kundennr_);


  gs.log("sys_id von ?: " + gr1.sys_id);



  return gr1.sys_id; // returning the sys_id


})(source);



"answer" needs some value to be returned else it won't work.



Hope this helps



Regards,


Dhruv


Thanks Chandan,



It is resolved now.


Ellie


Jim Coyne
Kilo Patron

Your script should be:



answer = (function transformEntry(source) {


  var response = "";   //the value that will be returned and set in the target field



  var gr1 = new GlideRecord("core_company");


  gr1.addQuery("u_company", source.getValue("u_kundennr_"));


  gr1.query();


  if(gr1.next()){


      response = gr1.getValue("sys_id");


  }


  gs.log("Kundennummer: " + source.u_kundennr_);


  gs.log("sys_id von ?: " + gr1.sys_id);



  return response; // the answer is returned here


})(source);



That will return the sys_id of the Company record to the "answer" variable which in turns sets the value of the target field.   What you were doing earlier was setting the value of target.u_company but then clearing it out with "return ''".



You also want to force the returned value to a string on line 8 with "getValue" instead of a   pointer to the data, which can change.


You should also be able to do it without a script by setting the Source field and specifying the field name in the reference table that you need to match on:


find_real_file.png


Hi Jim,



Thank you for helping. I is sorted now the reference field has u_compnay al along which is correct.


I specified the incorrect field in my addQuery and I did not see this anymore but Nish got me in the right direction.



Many Thanks


Ellie