can we sum this values and get the result?
# suitescript
m
can we sum this values and get the result?
n
Not sure this is the right channel to ask, isn't the sublist based on a regular search?
m
no this is a custom record form.. child record!
n
So wrong channel, you might get better answers if asking in #C298P0BCK or somewhere else 🙂
m
ok..
b
You can sum the percentages with a script, but im not sure what you want to do with them
m
after getting the sum of value entered in the percentage field . it should not more than 100
b
You still havent said what you want to do
You only mentioned the conditions in which to do it
Do you want to throw an error
Show an alert
m
yeah exactly
b
A user event script can throw an error serverside
A client script can show an alert or prevent adding new lines clientside
m
i want the client script only
b
use the validateLine entry point to trigger a function that runs when a line is added
use it to sum all the values in your percentage column
show an alert and return false if the sum fails validation
m
ok.. but its better to have some sample code??
please?
b
which one of those tasks did you need help with?
m
sum all the values in the percentage column
b
do you know how to get sublist values?
m
for (var i = 0; i <= lines; i++) { // If we are on the first item line. if (i === 0) { if (i === currentLine) { total = (parseFloat(currentRecord.getCurrentSublistValue({ sublistId: "recmachcustrecord170", fieldId: "custrecord168" })) || 0); } else { total = (parseFloat(currentRecord.getSublistValue({ sublistId: "recmachcustrecord170", fieldId: "custrecord168", line: i })) || 0); }
is this correct?
b
im not sure what you are doing with
Copy code
if (i === 0) {
m
selecting the first line of sublist
b
you are using it as the topmost block in your for loop
which basically means your code only runs when i is 0
m
okok i understand
sir it is not sum the value correct.. here is my code
define(['N/search'], function (s) { // 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 }; });
b
you still have strange logic
i dont know what you are doing with different logic for the first line
and now there is some weird stuff with firstItemNegative
beyond that, your code is unnecessarily complicated if you are stopping the user at saveRecord
you could move the summing logic from fieldChanged to saveRecord and ignore using getCurrentSublistValue and only use getSublistValue
m
ok sir will change the entire code and test it.
define([], function() { function saveRecord(context) { var currentRecord = context.currentRecord; var sublistName = context.sublistId; var sublistFieldName = context.fieldId; var currentLine = context.line; var lines = currentRecord.getLineCount({sublistId: 'recmachcustrecord170'}); var total; var A = 0; for (var i = 0; i <= lines; i++) { total = (parseFloat(currentRecord.getSublistValue({ sublistId: "recmachcustrecord170", fieldId: "custrecord168", line: i })) || 0); A= A+total; } if ( A > 100 ) { alert ('the overall percentage should be within 100'); return false; } return true; } return { saveRecord: saveRecord }; });
sir. now the above code showing error like sss_invalid_sublist_operation
b
Copy code
i <= lines;
that would go beyond existing lines for 0 based indexes
use
Copy code
i < lines;
m
found it sir..now its working fine. having another one doubt?
b
nope, looks great