Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

nikita_mironov
Kilo Guru

Coalesce by sys_id!

Back 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 non-existing sys_id?

Well, 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 a new unique value to it's sys_id.

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 find a cmdb record with matching "correlaton_id", then if I fail I would like 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 standart field maps, right?

Here is the way:

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.

Have a great day!

Comments
Chris M3
Tera Guru

Nikita,



Has this turned out to be 100% reliable?   I did something similiar recently, copying the OOB SCCM code to match to a CI, which returns a -1 to insert a new value.   It ended up actually creating a record with that sys_id, which I needed ServiceNow intervention to delete.   This in our production environment after working fine in DEV & QA.   I'm considering doing a quick patch to use a different value.


nikita_mironov
Kilo Guru

Hi Chris


Thanks for the feedback. sys_id = "-1" might be tricky as it is a reserved value for inserting a new record.


I already used the above mentioned approach in a dozen of transform maps - works stable and good.



Replace your "-1" with "abrakadabra" - this should make the magic


Chris M3
Tera Guru

Thanks, and I confirmed this does work.   I'm going to see if I can simulate how the -1 might have failed and push for Service-now to update their OOB code for SCCM if I can reproduce it.


Version history
Last update:
‎04-13-2015 08:18 PM
Updated by: