I am trying to update a field on the order lines. ...
# suitescript
n
I am trying to update a field on the order lines. When order is created via UI, it works fine. When WebService is used, it doesn't find the item unit/sale unit value. Returning empty results in lookupfields. Why? I am using user event after submit. Tried before submit as well.
b
first guess is permission issues
n
Run As Admin on UE.
It is not throwing permission error. It doesn't even read the unit from the sales order line item.
b
what does the code look like
n
The code works when created via UI. Let me share the code.
Copy code
var totalItem = rec.getLineCount({sublistId: 'item'});
for (var i = 0; i < totalItem; i++) {
   try{
      var itemId = rec.getSublistValue({sublistId: 'item', fieldId: 'item', line: i});
      var uomFactor = rec.getSublistValue({sublistId: 'item', fieldId: 'unitconversionrate', line: i});
      log.debug('uomFactor', uomFactor);
      if (!uomFactor) {
         /* This was blank that's why I am using lookup fields.
          var itemUnit = rec.getSublistValue({sublistId: 'item', fieldId: 'units', line: i});
          if (!itemUnit) {
          log.audit('itemUnit -- not found', itemUnit);
          continue;
          }*/
         var lookupResult = search.lookupFields({type: search.Type.ITEM, id: itemId, columns: ['unitstype', 'saleunit']});
         log.debug('lookupResult', lookupResult);
         var itemUnit = lookupResult.saleunit[0]?.value;
         var unitsTypeRec = record.load({type: 'unitstype', id: lookupResult.unitstype[0]?.value});
         var uomLength = unitsTypeRec.getLineCount('uom');
         for (j = 0; j < uomLength; j++) {
            var uomId = unitsTypeRec.getSublistValue({sublistId: 'uom', fieldId: 'internalid', line: j});
            if (uomId == itemUnit) {
               uomFactor = unitsTypeRec.getSublistValue({sublistId: 'uom', fieldId: 'conversionrate', line: j});
               break;
            }
         }
      }
      log.audit('uomFactor -- final', uomFactor);
      var origQty = rec.getSublistValue({sublistId: 'item', fieldId: 'custcol_bi_original_qty', line: i});
      if (!origQty) {
         log.audit('origQty -- not found', origQty);
         continue;
      }

      if (uomFactor) {
         rec.setSublistValue({sublistId: 'item', fieldId: 'quantity', line: i, value: (origQty / uomFactor)});
      }
   }
   catch (e) {
      log.error('Error::for loop', e);
   }

}
b
still not enough information
dont know what the rec variable is
if its the newRecord, then the normal choice is to actually load the record
n
rec is the loaded sales order record.
b
Works for me
Copy code
/**
 * @NApiVersion 2.0
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(["N/record", "N/runtime"], function (record, runtime) {
  return {
    afterSubmit: function (context) {
      log.debug("executionContext", runtime.executionContext);
      var rec = record.load({
        type: context.newRecord.type,
        id: context.newRecord.id,
      });
      var uomFactor = rec.getSublistValue({
        sublistId: "item",
        fieldId: "unitconversionrate",
        line: 0,
      });
      log.debug("uomFactor", uomFactor);
    },
  };
});
logs
n
Yes, for UI it is working for me as well.
When the order is created via Web Services, then it gets blank values.
b
execution context is not user interface
n
Sorry, missed the logs. Then why is it not working for my account? Configuration issues?
are you passing any parameters with your request?
b
pretty much the bare minimum necessary
Copy code
<soapenv:Envelope xmlns:soapenv="<http://schemas.xmlsoap.org/soap/envelope/>" xmlns:xsi="<http://www.w3.org/2001/XMLSchema-instance>">
    <soapenv:Header>
        <tokenPassport>
            <account>{{ACCOUNT_ID}}</account>
            <consumerKey>{{CONSUMER_KEY}}</consumerKey>
            <token>{{TOKEN_ID}}</token>
            <nonce>{{nonce}}</nonce>
            <timestamp>{{timestamp}}</timestamp>
            <signature algorithm="HMAC-SHA256">{{signature}}</signature>
        </tokenPassport>
    </soapenv:Header>
    <soapenv:Body>
        <add>
            <record xsi:type="tranSales:SalesOrder" xmlns:tranSales="urn:sales_2021_1.transactions.webservices.netsuite.com">
                <entity internalId="9"/>
                <itemList>
                    <item>
                        <item internalId="84"/></item>
                </itemList>
            </record>
        </add>
    </soapenv:Body>
</soapenv:Envelope>
👍 1