Pmac08
Pmac08

Reputation: 11

Accessing the Unit Cost field using a userevent script

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
  };
});

Upvotes: 0

Views: 154

Answers (0)

Related Questions