Parse a string after ' : ' and assign the value to reference field

Mrman
Tera Guru

Hi All,

We have a requirement to parse a string after ':' and we need to assign the value after ':' to a reference field called 'server' in cmdb_ci_db_instance .

To be more clear, we get the HOSTNAME in the source field as "ORACLE:CNSZIDC04" . We need to parse after ':' and take the value after that and assign to a field called Server.

We are planning to write a OnAfter transform script for this. Please guide how to parse and take the value to the right side of ':'. Please note the Target field is Reference field . Please guide.

CODE:

============

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

  var shnm = source.u_hostname;

  var scinm = source.u_cinum;

  var pse = shnm.split('\:');

  var str1 = pse[0]+':'+pse[1];

  var grd = new GlideRecord('cmdb_ci_db_instance');

  grd.addQuery('u_maximo_uid', 'scinm');

  grd.query();

  if(grd.next()){

  grd.u_server = pse[1].sys_id;

  }

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

5 REPLIES 5

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

You can actually do this in a field map.   When the target field is a reference type field, you can set the "Referenced value field name" to the field in the referenced record to match which I believe in your case is "u_maximo_uid".



Then check the Use source script checkbox and enter the following script:


answer = source.u_cinum.toString().split(":")[1];



find_real_file.png



Edit: Also with reference and choice type fields you can set the Choice action to determine what to do if a target reference record or choice is NOT found.   You can set it to create, ignore, or reject.   Really up to you on what you need to do in this situation.


Hi Michael,



Actually the incoming data we get has the HOSTNAME sometimes not following correct format(no colon separating the DB name from the Server).



In Such cases we should load the data , instead throw an exception.



Also, The Target Reference field is called Server. and it is not the Target table as mentioned in my Transform map. I need to set the Server in cmdb_ci_db_instance which extends cmdb_ci. And cmdb_ci is my atrget table in transform map.



Please suggest how can I use Source script , because I cannot see that field in cmdb_ci.


Ok in cases where you where you want to throw an exception setting the Choice action to "reject" will cause the entire row from the import set table to be rejected, which sounds like what you want.



Set the target field to "Server", but then my question is what field in the cmsb_ci_db_instance table (field name) do you need to match with the hostname provided by the imported data.   "CNSZIDC04" was your example in the original post.   Figure out what the column/field name is that contains that value and then enter that field name in the "Referenced value field name" field.   By default with references it will either match based on the Display Value or the SysID.


Ankur Bawiskar
Tera Patron
Tera Patron

Hi Ravishankar,



Have field map for that field. Set the target field and mention the reference field name. Have source script checkebox checked and write the script


Also ensure to keep choice action as reject. If you have it as create then if value not found it will create new record in referenced field.



Script to use:


var incomingValue = source.u_cinum;



answer = incomingValue.split(":")[1];



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur



Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader