0

On the sublist on my proposal, i have 2 lines with specific items on them that I need the script to look for and update the unit cost based on a certain percentage of a custom field on the header level. Not sure where my script is messing up.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/record'], function(record) {
  function beforeSubmit(context) {
    if (
     context.type === context.UserEventType.CREATE ||
      context.type === context.UserEventType.EDIT
    ) {
      var currentRecord = context.newRecord;

      var subtotal = currentRecord.getValue({
        fieldId: 'subtotal'
      });
      var installEstPercent = currentRecord.getValue({
        fieldId: 'custbody_percent_field'
      });

      // Calculate the new unit cost by multiplying the subtotal and installEstPercent
      var newUnitCost = subtotal * installEstPercent / 100;

      // Loop through the lines of the item sublist and set the rate field for the CBI.ESTIMATE and   EXT.LABINST items
      var lineCount = currentRecord.getLineCount({
        sublistId: 'item'
      });
      for (var i = 0; i < lineCount; i++) {
        var itemId = currentRecord.getSublistValue({
          sublistId: 'item',
          fieldId: 'item',
          line: i
        });
        if (itemId === 'Item1' || itemId === 'Item2') {
          // Set the rate field to the new unit cost
          currentRecord.setSublistValue({
            sublistId: 'item',
            fieldId: 'rate',
            line: i,
            value: newUnitCost
          });
        }
      }  
   }
  }

  return {
    beforeSubmit: beforeSubmit
  };
});
Pmac08
  • 11
  • 1
  • 1
    The rate is sourced from the item record based on the Price Level and other pricing rules, so you may need to override that by setting the item line price level to Custom (id: -1 IIRC) before attempting to set the rate. – Krypton Feb 20 '23 at 18:39
  • the price level is already set to custom on my line items. – Pmac08 Feb 20 '23 at 19:15
  • 1
    OK, probably not that then :) Have you confirmed your `if` condition is evaluating to `true`? Try placing a `log.debug()` immediately after the line `if (itemId === 'Item1' || itemId === 'Item2') {` to ensure the script execution is entering that block. I assume the `'Item1'` and `'Item2'` are just placeholders, but ensure you're not comparing internal ids to names/SKUs etc, and also that you're not comparing a number represented as a string with an actual number, seeing you're using strict equality (`===`). If it's not evaluating to `true`, log the values you're comparing to see why. – Krypton Feb 20 '23 at 22:06
  • 1
    I had the actual Item name listed in my script. Changing it to the internal ID made the script work. Thanks!!! – Pmac08 Feb 21 '23 at 14:44

0 Answers0