Conditional Script for setting coalesce field in Transform Map

Mahak2
Kilo Guru

Hello guys,

At present I am using the Serial Number as Coalesce field in my Transform Map for Integration .

Now the problem is there are some CIs in my instance whose Serial Number is either empty or is using the different type of serial number as created by discovery which is Baseboard .And my data source is bringing the Bios Serial Number.

And hence due to these duplicates are being created. 

To solve this I am thinking to write the script 

As per that first priority for coalesce will be Serial Number .

But if it is empty or not matches I want to check the name of the Ci as 2nd option of coalesce .

How to achieve this ?

Can anyone help?

 

Regards ,

Mahak

1 ACCEPTED SOLUTION

Hi Mahak,

if you have removed the coalesce then the action == 'update' won't work

also action == "insert" won't work

try to modify the code as suggested by me earlier

target object won't give you the target field value as you have removed the field map and coaslece

Regards
Ankur

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

View solution in original post

8 REPLIES 8

Hi,

sample script below

var query = 'serial_number=' + source.u_bios_serialnumber + '^ORname=' + t + '^ORdns_domain=' + source.u_computer_domain_name;

var gr = new GlideRecord('ABC');
gr.addEncodedQuery(query);
gr.query();
if(gr.next())
{

// update existing record into target and ignore

gr.field1 = source.u_field1;
gr.field2 = source.u_field2;
gr.update();

ignore = true;
}
else
{
// insert new record into target and ignore

var gr = new GlideRecord('ABC');
gr.initialize();
gr.field1 = source.u_field1;
gr.field2 = source.u_field2;
gr.insert();
}

Regards
Ankur

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

Hello Ankur,

Thank you for the help.

I have written the onbefore script and remove the Coalesce from Serial Number .

But event after that its not checking the fqdn of cis as the 2nd priority and creating the duplicates even if the serial number is different but if fqdn is same it should not insert any new record.

Also it not copying the concatenated value in fqdn field after inserting.

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


// Add your code here
if(action == "update" && source.u_bios_serialnumber == target.serial_number && target.discovery_source != "ABC")
{


ignore=true;

}
else if(action == "insert" && (source.u_operation_system == 'Windows Server 2008 Enterprise Edition')||(source.u_operation_system == 'Windows Server 2008 R2 Standard Edition')||(source.u_operation_system == 'Windows Server 2008 Standard Edition')||(source.u_operation_system == 'Windows Server 2012')||(source.u_operation_system == 'Windows Server 2012 Datacenter Edition'))
{
var t = source.u_computer_name.toString();
t = t.toUpperCase();
t = t.split('.')[0];
var bb = source.u_computer_domain_name.toString();
var aa = t +'.' + bb ;


var que = 'serial_number=' + source.u_bios_serialnumber + '^ORfqdn=' + aa;
var gr = new GlideRecord('ABC');
gr.addEncodedQuery(que);
gr.query();
if(gr.next())
{
ignore = true;

}
else
{
var gg = new GlideRecord('ABC');

var cc = source.u_computer_name.toString();
cc = t.toUpperCase();
cc = cc.split('.')[0];
var dd = source.u_computer_domain_name.toString();
var ee = cc +'.' + dd ;

gg.initialize();

gg.disk_space = source.u_disk_total_space;
gg.dns_domain = source.u_computer_domain_name;
gg. ip_address = source.u_ip_addr1_text;
gg.mac_address = source.u_mac_addr1;
gg.name = t;
gr.fqdn = ee;

gg.insert();

}

}

else
{
ignore = true;
}

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

 

Can you please help?

 

Regards,

Mahak

Hi Mahak,

if you have removed the coalesce then the action == 'update' won't work

also action == "insert" won't work

try to modify the code as suggested by me earlier

target object won't give you the target field value as you have removed the field map and coaslece

Regards
Ankur

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

Hi @Ankur Bawiskar ,

Same issue i am also facing, if state is cancelled then it should ignore but instead of that its creating, I Just followed the same which you said but its still updating the record which is already existed CI record with in the cancelled state .