// Client Script global variables.
var allowSave = true;
var firstItemNegative = false;
function fieldChanged(context) {
var currentRecord = context.currentRecord; // Current opened record.
var sublistName = context.sublistId; // The internal ID of the sublist.
var sublistFieldName = context.fieldId; // The internal ID of the field that was changed.
var currentLine = context.line; // Line number (first line has value = 0) of Item User is on.
// Run when the Item field of the inventory sublist changed.
// Item for some reason does not fire a change event, so using item description instead.
// This means the description has to be required for these types of items.
if (sublistName === 'recmachcustrecord170' && sublistFieldName === 'custrecord168') {
// Check how many lines exist in the inventory sublist.
var lines = currentRecord.getLineCount({sublistId: 'recmachcustrecord170'});
alert(lines);
var total; // Total used to check whether sum of quantities is zero.
var quantity; // Used to hold quantity for current line item.
var percentsum;
for (var i = 0; i <= lines; i++) {
// If we are on the first item line.
if (i === 0) {
if (i === currentLine) {
// Get the first item line's Adjust Qty. By field value.
// Note that the value could be invalid in which case 0 is used.
// For partially entered lines.
total = (parseFloat(currentRecord.getCurrentSublistValue({
sublistId: "recmachcustrecord170",
fieldId: "custrecord168"
})) || 0);
} else {
// Get the first item line's Adjust Qty. By field value.
// For completed lines that have been Added.
total = (parseFloat(currentRecord.getCurrentSublistValue({
sublistId: "recmachcustrecord170",
fieldId: "custrecord168",
line: i
})) || 0);
}
// If the quantity of the first line is positive then this is a real Inventory Adjustment
// and not a roll that was cut into smaller inventory.
if (total >= 0) {
firstItemNegative = true;
} else {
firstItemNegative = false;
}
} else if (i > 0) { // For non-first lines.
if (i === currentLine) {
// Get the current item line's Adjust Qty. By field value.
quantity = (parseFloat(currentRecord.getCurrentSublistValue({
sublistId: "recmachcustrecord170",
fieldId: "custrecord168"
})) || 0);
} else {
// Get the current item line's Adjust Qty. By field value.
quantity = (parseFloat(currentRecord.getCurrentSublistValue({
sublistId: "recmachcustrecord170",
fieldId: "custrecord168",
line: i
})) || 0);
}
// If the first item is negative then we have to keep a running total of the quantities.
if (firstItemNegative) {
total = total + quantity;
alert(total);
}
} // if (i === 0)
} // for (var i = 0; i < lines + 1; i++)
// If the total of the quantities are not zero then error. Allow if only the first line exists.
if (total > 100 ) {
allowSave = false;
} else {
allowSave = true;
}
}
} // fieldChanged
function saveRecord() {
if (!allowSave) {
alert("Error: The percentage must not be greater than 100.");
}
return allowSave;
} // saveRecord
return {
fieldChanged: fieldChanged,
saveRecord: saveRecord
};
});