Coalesce by sys_id!

creBack to transform maps in #Servicenow! Ever thought what would happen if you coalesce by sys_id? Well, it is not so interesting for matched records as it would simply update it, but what would happen if you supply a non-existing sys_id?

The new record will be created for non-matched value supplied for sys_id but if you look at the created record then you will see that system assigned “sys_id” with a new unique value.

So what?

How can you benefit from it?

More and more I face the need to perform “smart” coalesce in #servicenow Transform Maps.
For example I want to first attempt finding a cmdb record with matching “correlation_id”, then if I fail then I want to search by “name” and finally (if I did not find anything at two previous steps) I will create a new record. Its not easy to accomplish with standard field maps coalesce, isn’t it?

Here is the way (and we will use the trick with non-matching sys_id value):

Use [script]  and coalesce to target record’s sys_id with the script looking like this:

var gr = new GlideRecord(‘cmdb_ci’);

// 1. Lookup by correlation_id

if (gr.get(‘correlation_id’, source.u_correlation_id)){

  answer = gr.sys_id;

} else

// 2. Lookup by name

if (gr.get(‘name’, source.u_name)){

  answer = gr.sys_id;

} else {

 answer = ‘abrakadabra’; // Here is the trick! I did not find anything so I return the value that for sure will not be present in target table as sys_id!

}

Of course to make it working for repeated uploads you need to populate “name” and “correlation_id” in your transformations.

Enjoy and  have a great day!

This post was originally published on LinkedIn.

mm
Nikita is a certified ServiceNow Developer and Instructor and one of Fruition Partners (ex-Aspediens) Senior Consultants. He has a strong experience in architecture, programming and integration and worked on multiple large international deployments. If asked to sum up his job in a few words, Nikita will invariably smile and tell you "I deliver!"
Recent Posts

Leave a Comment

Start typing and press Enter to search