Anybody knows how to sum the values of a field in ...
# general
m
Anybody knows how to sum the values of a field in each line the sublist??
f
You can try it by a script.
m
yeah sir i have the script but it not sum the values
f
What is the code ?
m
// 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 }; });
f
I’m seeing your script. First of all, try to change the less equal by less (<= by < in the for)
m
in for loop?
f
Ya
That it’s wrong, if not working when I have time I will try to see what you are trying to do to on the script.
m
ok
f
I was seeing that you are using the current record of the client script. In some cases, you are trying to access the value using the line as a parameter of the getCurrentSublistValue. That method doesn’t accept that parameter on your own method.
h
You should move the calculation to saveRecord.