Convert RAM field on cmdb_ci_computer table from bytes to GB
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2024 02:20 AM
Hi all,
We're looking to import workstations are part of our CMDB from a 3rd party tool, we have the workstations imported fine but the RAM comes in the format of bytes:
Can this be converted to GB within ServiceNow maybe via a business rule or this something that would need to be done in the 3rd party tool before being imported.
Many thanks,
Alex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2024 03:37 AM
Hi @Alex Saager1 ,
You can do this using a BR or an existing transform map within which you can write a transform script to achieve this using the script below:
var bytes = 68718940160;
function readableBytes(bytes) {
var i = Math.floor(Math.log(bytes) / Math.log(1024)),
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
return (bytes / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + sizes[i];
}
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2024 10:49 PM
Hi @Alex Saager1 ,
Would like to modify above script provided, where I forgot to include the line to call the function as well to get this working. Please use below script and should work for you:
var bytes = 68718940160;
readableBytes(bytes);
function readableBytes(bytes) {
var i = Math.floor(Math.log(bytes) / Math.log(1024)),
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
gs.info(bytes / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + sizes[i];
return (bytes / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + sizes[i];
}
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2024 08:54 AM
Hi @shloke04
So I've tried adding the script to the transform map by using the "run script" checkbox AND the transform script related links tab (not at the same time) but the ram is still showing incorrectly.
Script
Test loaded 20 records and checked the import set:
Opened one of the records stills showing same im afraid:
Am I missing anything?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 12:03 AM
Hi @Alex Saager1 ,
Yes, script was an sample script which I provided. Now when you are using within your transform script you would need to pass the source field name from your Import set table on line number 3 where currently the bytes is hard coded and also set the target attribute on the last line.
I have updated the script, just replace the correct field name from both source and target table and you will be good to go.
So your updated script will look like below:
var bytes = source.field_name; // Replace "field_name" with name of the field from your Import set table selcted on your transform map which has the bytes value.
readableBytes(bytes);
function readableBytes(bytes) {
var i = Math.floor(Math.log(bytes) / Math.log(1024)),
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
gs.info(bytes / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + sizes[i];
target.field_name = bytes / Math.pow(1024, i).toFixed(2) * 1 + ' ' + sizes[i]; // Replace "field_name" with your field present on target table where this value needs to be updated to
}
Regards,
Shloke