Record exists using GlideRecord

maryc
Tera Contributor

Hi,

I am connecting to external app using REST and am parsing the response. Before inserting into CMDB, I want to check if the record exists and am trying to use this query.

var isinsert=false;
var commrecord = new commrecord("cmdb_ci_comm");
commrecord.addQuery("asset_tag",parsed_object.AssetNumber);
commrecord.query();
//gs.log(" isinsert "+isinsert);
if(commrecord.next()){
    isinsert=false;
}else{
    isinsert=true;
    commrecord.initialize();
}

If I execute this script as background script hard coding the asset number, it works perfectly. While parsing the REST response, it always says its a new record and inserts duplicate records.

Any help here is appreciated,

 

 

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

So it means the value from parsed_object.AssetNumber is not properly parsed

Did you try to log that value?

gs.info('Asset Number is: ' + parsed_object.AssetNumber);

Share the json request you are receiving so that we can help parsing that

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

maryc
Tera Contributor

Here is the response

{"Devices":[{"EasIds":{"EasId":["JIACBL73CP5333KREO60500PQENV8"]},"Udid":"4dd942b336acfa682882e78bda932c4278285936f2","SerialNumber":"DMPQ3BQW2TG5WQ","MacAddress":"94E96AAVC7AF2","Imei":"356968064761693","EasId":"","AssetNumber":"4dd93232a682882e78bda932c4278283335936f2","DeviceFriendlyName":"BrinkP iPad G5WQ","LocationGroupId":{"Id":{"Value":591},"Uuid":"6333289ea19-8845-4bab-8dfe-171be8613ce3","Name":"ABC"},"LocationGroupName":"ATR","UserId":{"Id":{"Value":40340},"Uuid":"c39333a8ea9-84e8-439d-9f30-dd5236597f59","Name":"Brink"},"UserName":"HELLO","DataProtectionStatus":0,"UserEmailAddress":"aaa@aa.com","Ownership":"C","PlatformId":{"Id":{"Value":2},"Name":"Apple"},"Platform":"Apple","ModelId":{"Id":{"Value":2},"Name":"iPad Air 2 LTE (16 GB Space Gray)"},"Model":"iPad Air 2 LTE (16 GB Space Gray)","OperatingSystem":"13.3.1","PhoneNumber":"12354559544","LastSeen":"2020-03-05T09:24:34.563","EnrollmentStatus":"Enrolled","ComplianceStatus":"Compliant","CompromisedStatus":false,"LastEnrolledOn":"2018-10-09T14:28:03.700","LastComplianceCheckOn":"0001-01-01T00:00:00.000","LastCompromisedCheckOn":"2018-10-05T19:02:31.993","IsSupervised":false,"VirtualMemory":0,"DeviceCapacity":11084263424.0,"AvailableDeviceCapacity":6433484800.0,"IsDeviceDNDEnabled":false,"IsDeviceLocatorEnabled":false,"IsCloudBackupEnabled":false,"IsActivationLockEnabled":true,"IsNetworkTethered":false,"BatteryLevel":"20 %","IsRoaming":false,"SystemIntegrityProtectionEnabled":false,"ProcessorArchitecture":0,"TotalPhysicalMemory":0,"AvailablePhysicalMemory":0,"DeviceCellularNetworkInfo":[{"CarrierName":"AT&T","CardId":"890141268545208244038","PhoneNumber":"1444455944","DeviceMCC":{"SIMMCC":"","CurrentMCC":"310"},"IsRoaming":false}],"Id":{"Value":37528},"Uuid":"40f55cf4-1a52-427b-9d4439-9a40eeetb969c"}],"Page":1,"PageSize":1,"Total":16}

 

Hi,

the asset number from json seems to be the sys_id of the record; so update code as below for parsing; also ensure the column for query to the table cmdb_ci_comm is correct; currently it is asset_tag. Is that a reference field?

var parsedData = JSON.parse(str);

var val = parsedData.Devices[0].AssetNumber;

commrecord.addQuery("asset_tag",val);

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

maryc
Tera Contributor

The same thing happens if I query for Serial number or MAC address