The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Transform Script to assign a replaced string

Mrman
Tera Guru

Hi All,

We are having the below field map to map   HOSTNAME from source table to Name in Target table. Now we want only for class "cmdb_ci_db_instance" in Source table the hostname which has   like (ORACLE:CNSZIDC04) , we need to replace : with @   and set to name field in Target like(ORACLE@CNSZIDC04)   . We have the below script , please guide.

Does it conflict with the existing Field Map.

find_real_file.png

I tried the below script it is not setting the Name by replacing : with @ .In logs I am getting correctly as Replaced String: ORACLE@CNSZIDC04

Still getting as

find_real_file.png

Code:

=====

CODE:

=======

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

  var sdb = source.u_cmdb_class_name;

  if(sdb == "cmdb_ci_db_instance"){

  var hnmd = source.u_hostname;

  var resd = hnmd.replace(":", "@");

  gs.log("Replaced String: "+resd);

  target.name = resd;

  }

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

1 ACCEPTED SOLUTION

Justin Abbott
Giga Guru

It looks to me like you may be running your code as an onStart Transform Script. I ran the following code in a Field Map, using the 'Use source script' option, and it worked as expected.



answer = (function transformEntry(source) {


  var sdb = source.u_cmdb_class_name;


  if(sdb == "cmdb_ci_db_instance"){



  var hnmd = source.u_hostname;


  var resd = hnmd.replace(":", "@");


  gs.log("Replaced String: "+resd);


  return resd;


  } else {


  return source.u_hostname;


  }



})(source);


View solution in original post

7 REPLIES 7

Deepak Ingale1
Mega Sage

Hi Ravishankar,



You may remove out the field map and have name field set via transform map script



if ( sourceClassIsDesired ) {


  }


else {


target.name = source.u_hostname;


}


Justin Abbott
Giga Guru

It looks to me like you may be running your code as an onStart Transform Script. I ran the following code in a Field Map, using the 'Use source script' option, and it worked as expected.



answer = (function transformEntry(source) {


  var sdb = source.u_cmdb_class_name;


  if(sdb == "cmdb_ci_db_instance"){



  var hnmd = source.u_hostname;


  var resd = hnmd.replace(":", "@");


  gs.log("Replaced String: "+resd);


  return resd;


  } else {


  return source.u_hostname;


  }



})(source);


Hi Justin,



Please let me know how do I stop inserting if Hostname value on the Database record doesn't follow the correct format (no colon separating the DB name from the Server)



an exception should be thrown and the record should not be loaded.


if (hnmd.indexOf(':') == -1) {


  ignore = true;


}