I am trying to add discount using custom column, I...
# suitescript
j
I am trying to add discount using custom column, I found a solution in suiteanswer but seems like something is missing on my script below: function userEventAfterSubmit(type){ recType = nlapiGetRecordType(); recID = nlapiGetRecordId(); var recLoad = nlapiLoadRecord(recType, recID);         var itemcnt = recLoad.getLineItemCount('item');         var itemdiscnt = recLoad.getLineItemCount('item');         var linenumber;     for (var i = 1; i <= itemcnt;i++) {      var itemType = recLoad.getLineItemValue('item', 'itemtype', i);      var itemId = recLoad.getLineItemValue('item', 'item', i);      if (itemType == 'InvtPart') {      linenumber = i; for (var j = i; j <= itemcnt; j++) { itemId = recLoad.getLineItemValue('item', 'item', j); itemType = recLoad.getLineItemValue('item', 'itemtype', j); if(itemType!='InvtPart') {      if (itemId == '672') { //Internal ID of the Discount Item      var dct = recLoad.getLineItemValue('item', 'rate', j);      recLoad.setLineItemValue('item', 'custcol_row_discount', linenumber, dct);      i++;      }      }      else if(itemType=='InvtPart' && i!=linenumber){      i++;      break;      }//end if inventory part      } // end of second loop      }//first if      }//first for loop     var id = nlapiSubmitRecord(recLoad, true); } // end function function isNullOrEmpty(val) { return (val == null || val == '' || val == undefined); }
b
that is terrible code, whoever wrote that made a unnecessary nested loop
👍 2
Copy code
// consider if a before submit user event is appropiate
// loading the current record to make changes basically doubles save time
function userEventAfterSubmit(type) {
var recLoad = nlapiLoadRecord(nlapiGetRecordType(), nlapiGetRecordId());
var itemcnt = recLoad.getLineItemCount("item");
var lastInventoryItemLine;

for (var i = 1; i <= itemcnt; i++) {
  var itemType = recLoad.getLineItemValue("item", "itemtype", i);
  var itemId = recLoad.getLineItemValue("item", "item", i);

  // to be clear, this code fails miserably if there are items that aren't
  // inventory items
  if (itemType === "InvtPart") {
    lastInventoryItemLine = i;
  } else if (itemId == "83" && lastInventoryItemLine) {
    // you might want to make this a sum
    // you might also want to handle the case where the first line is a discount
    // amount is much safer than rate
    recLoad.setLineItemValue(
      "item",
      "custcol_row_discount",
      lastInventoryItemLine,
      recLoad.getLineItemValue("item", "amount", i)
    );
  }
}

nlapiSubmitRecord(recLoad, true);

}
you dont use nlapiLoadRecord for before submit scripts, you just get the current record using nlapiGetNewRecord and make change to it
👍 3
n
OK my bad I misread the script somehow as beforeSubmit 🤨 deleting embarrassing comment... 🤭
👍 2
j
Thanks everyone! Found an easier way using client script.