/**
* @NApiVersion 2.x
* @NScriptType ClientScript
*/
define(['N/log', 'N/currentRecord', 'N/search', 'N/record'], function(log,
currentRecord, search, record) {
/**
* Function to be executed when saving the record (saveRecord).
* @param {Object} scriptContext
*/
function saveRecord(scriptContext) {
// Get the current record
var currentRecordObj = currentRecord.get();
// Read the amount field from the Bill record
var billAmount = parseFloat(currentRecordObj.getValue({
fieldId: 'usertotal'
}));
// Read the Purchase Order number field from the Bill record
var poNumber = currentRecordObj.getValue({
fieldId: 'custbody_cf_po_no'
});
if (poNumber) {
// Search for the Purchase Order based on the Purchase Order Number
var poSearch = search.create({
type: search.Type.PURCHASE_ORDER,
filters: ['tranid', search.Operator.IS, poNumber]
});
var poSearchResults = poSearch.run().getRange({ start: 0, end: 1 });
if (poSearchResults.length > 0) {
// Fetch the Purchase Order record based on the internal ID
var poInternalId = poSearchResults[0].id;
var poRecord = record.load({
type: record.Type.PURCHASE_ORDER,
id: poInternalId
});
// Get the 'total' field value from the Purchase Order record
var poTotal = parseFloat(poRecord.getValue({
fieldId: 'total'
}));
// Log the amount, PO number, and PO total
log.debug({
title: 'Bill Save Event',
details: 'Bill record entered. Amount: ' + billAmount + ', PO
Number: ' + poNumber + ', PO Total: ' + poTotal
});
// Compare the amounts
if (poTotal !== billAmount) {
// Raise an alert if amounts don't match
alert("Bill Amount doesn't match with Purchase Order!");
return false; // Cancel the save operation
}
} else {
// Raise an alert if no matching Purchase Order is found
// alert("No Purchase Order (PO) found with this number!");
// return false; // Cancel the save operation
}
} else {
// Raise an alert if no PO number is present
// alert("No Purchase Order (PO) number is associated with this
Bill!");
// return false; // Cancel the save operation
}
return true; // Allow the record to be saved
}
return {
saveRecord: saveRecord
};
});