Covert GB into MB using transform script

ChrisSN
Tera Contributor

I have an excel sheet that has the ram in GB and I need it covered to MB on the computer cmdb_ci_computer table. The title on the excel is "DEVICE_MEMORY" and the values show as "10GB". I tried the script below but no luck, any advice? 

 

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

 

var deviceMemoryInt = parseInt(source.u_device_memory, 10);

 

if (!isNaN(deviceMemoryInt)) {

 

target.ram = deviceMemoryInt * 1024;

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

1 ACCEPTED SOLUTION

Do you have the ram field on your field map?  If so that maybe whats causing the issue with your script.

 

It would probably be easier to use a field mapping script.  So add a field mapping and check the "Use source script" and use that script to do the calculation and then just return the value.

View solution in original post

6 REPLIES 6

DrewW
Mega Sage
Mega Sage

When are you running the script?  OnBefore, OnAfter or something else?

Have you tried using getValue and setValue?

var deviceMemoryInt = parseInt(source.getValue("u_device_memory"), 10);
if (!isNaN(deviceMemoryInt)) {
   target.setValue("ram", deviceMemoryInt * 1024);
}

 

Also have you verified all of the field names to make sure they are correct?

 

ChrisSN
Tera Contributor

Ive tried "onStart" and "onComplete" and neither worked for me. As far as the field names go, I know the target field is "ram" and the source filed on the spreadsheet is "DEVICE_MEMEORY" but on the filed map it shows it as "u_device_memory". I Tried what you suggest with "onStart" and "onBefore" but no dice with either. Any other suggestions?

You need to use either an OnBefore script or you need to use a field mapping script.

ChrisSN
Tera Contributor

On before, still no dice. Im using the transform script, is there somewhere else I can try it?