Hey Everyone i want to get these info from the he...
# suitescript
l
Hey Everyone i want to get these info from the header Discount Item Rate and add them as line item Script :
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */
define(['N/record'], function(record) {

  function beforeSubmit(context) {
    if (context.type === context.UserEventType.CREATE || context.type === context.UserEventType.EDIT) {
      var newRecord = context.newRecord;

      var discountItemId = newRecord.getValue({
        fieldId: 'discountitem'
      });

      var discountRate = newRecord.getValue({
        fieldId: 'rate'
      });

      if (discountItemId && !isDiscountSublistItem(newRecord, discountItemId)) {
        try {
          var lineCount = newRecord.getLineCount({
            sublistId: 'item'
          });

          newRecord.selectNewLine({
            sublistId: 'item'
          });

          newRecord.setCurrentSublistValue({
            sublistId: 'item',
            fieldId: 'item',
            value: discountItemId
          });

          newRecord.setCurrentSublistValue({
            sublistId: 'item',
            fieldId: 'rate',
            value: discountRate
          });

          newRecord.setCurrentSublistValue({
            sublistId: 'item',
            fieldId: 'amount',
            value: 0
          });

          newRecord.commitLine({
            sublistId: 'item'
          });
        } catch (e) {
          log.error('Error : ', e);
        }
      }
    }
  }

  function isDiscountSublistItem(record, discountItemId) {
    var lineCount = record.getLineCount({
      sublistId: 'item'
    });

    for (var i = 0; i < lineCount; i++) {
      var item = record.getSublistValue({
        sublistId: 'item',
        fieldId: 'item',
        line: i
      });

      if (item === discountItemId) {
        return true;
      }
    }

    return false;
  }

  return {
    beforeSubmit: beforeSubmit
  };
});
this script is not working onCreate and Edit as well
n
Are you trying to add the discount item if it doesn't already exist on the lines?
l
@Nathan L Yes exaclty and once done i want to set the header = null
n
I mean the code looks good to me for adding the line. I don't see anything in there for clearing out the fields in the header though. Is it just not adding your line? can you log something inside your if block to make sure its hitting that point in the code
b
you need to nail down your problem beyond not working
first place to look is your script deployment
make sure the script is deployed to the correct record at the right log level and to the intended audience
second place to look is the code, you usually want a log at the beginning to easily tell if the script ran or not
if you see that log but the rest doesnt work, then the problem is the code
a quick glance at the code suggests that this is your first user event script, so my suggestion is to settle for a script that runs and logs that it ran