Created
December 28, 2021 03:16
-
-
Save marawan-nwh/14880c3be7f55a12bf78823a354d89b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
*@NApiVersion 2.x | |
*@NScriptType ClientScript | |
*/ | |
define(["N/error"], function (error) { | |
function round(num) { | |
return Math.round((num + Number.EPSILON) * 100) / 100; | |
} | |
function fieldChanged(context) { | |
try { | |
var currentRecord = context.currentRecord; | |
var sublistName = context.sublistId; | |
var sublistFieldName = context.fieldId; | |
log.audit("sublistFieldName", sublistFieldName); | |
if ( | |
sublistName === "item" && | |
(sublistFieldName === "quantity" || | |
sublistFieldName === "item" || | |
sublistFieldName === "rate" || | |
sublistFieldName === "custcol_conversion_rate") | |
) { | |
var convRate = currentRecord.getCurrentSublistValue({ | |
sublistId: "item", | |
fieldId: "custcol_conversion_rate", | |
}); | |
log.audit("convRate", convRate); | |
if (convRate) { | |
var quantity = currentRecord.getCurrentSublistValue({ | |
sublistId: "item", | |
fieldId: "quantity", | |
}); | |
log.audit("quantity", quantity); | |
if (quantity > 0) { | |
currentRecord.setCurrentSublistValue({ | |
sublistId: "item", | |
fieldId: "custcol_total_in_ton", | |
value: round(quantity * convRate), | |
}); | |
} | |
var rate = currentRecord.getCurrentSublistValue({ | |
sublistId: "item", | |
fieldId: "rate", | |
}); | |
log.audit("rate", rate); | |
var ratePerTon = rate / convRate; | |
currentRecord.setCurrentSublistValue({ | |
sublistId: "item", | |
fieldId: "custcol_edc_rate_per_ton", | |
value: round(ratePerTon), | |
}); | |
} | |
} | |
} catch (err) { | |
log.audit("Error", err); | |
} | |
} | |
return { | |
fieldChanged: fieldChanged, | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment