0
votes

I would like to group items of the same names in a sales order and consolidate the price amount in a report.

For eg. Sales Order

Item A 100usd 
Item A 100usd 
Item A 100usd 
Item A 100usd

On the report I want to sum up the total price of all items and display Item A as one line: Item A 400usd

I know I should be using for loop and array to do this, however it doesnt seem to work.

//scan through all lines
for(i=1;...){
item[i]=getitemforline(i);

itemprice[i]=getitempriceforline(i);

}
//check current line one by one for any duplicates, and sum up itemprice if there is
for(k=1;...){
      for(i=1;i<k;i++){ 
      currentitem[k] = getitemforcurrentline(k);
      currentitemprice[k] = getitempriceforcurrentline(k);
       if(currentitem[k] == item[i]){
       itemprice[i] = itemprice[i] + currentitemprice[k];
       }
      }
 print(itemw[i]+itemprice[i]);
}
2
This is pretty vague - what structure is the data currently in? What do the getitemforline() and other functions return? - nnnnnn
The data structure is in NetSuite DB table format. Getlineforitem(i, price) returns the value for a particular line. - user1033038
The function names are only pseudocodes and does not represent the exact function name. - user1033038

2 Answers

0
votes

Your for loop is incorrect.

for(i=1;i<k;i++){

You need for(i=1;i<=k;i++){

0
votes
Use below method for above use case:

var getLineItemAggregateResult = function(salesOrderId) {
    if(!salesOrderId) {
        return;
    }

    var salesOrder = nlapiLoadRecord('salesorder', salesOrderId); 
    var lineItemCount = salesOrder.getLineItemCount('item');

    /*-----------------------------------------------------------------------------------------------------------------------
    "lineItem_TotalValue" this is an object, it will contain lineitem as a key and total amount as a value
    e.g.: {item1 : 400,
          item2 : 500}
          As this will a JSON object so you can use this to display your final result.
    -------------------------------------------------------------------------------------------------------------------------*/
    var lineItem_TotalValue = {};
    for(var i = 1; i <= lineItemCount; i++) {
        if (!lineItem_TotalValue[salesOrder.getLineItemValue('item', 'item', i)]) {
            lineItem_TotalValue[salesOrder.getLineItemValue('item', 'item', i)] = parseInt(salesOrder.getLineItemValue('item', 'amount', i), 10);
        }
        else {
            lineItem_TotalValue[salesOrder.getLineItemValue('item', 'item', i)] += parseInt(salesOrder.getLineItemValue('item', 'amount', i), 10);
        }
    }

    return lineItem_TotalValue;
}