Hey everyone, I am trying to get the `expectedre...
# suitescript
k
Hey everyone, I am trying to get the
expectedreceiptdate
field from a sublist in a Purchase Order and apply it to the linked
Sales Order
items. When I try to get the Value of the date field of an item, it returns blank. How do I define the
record
module and require the
format
module in the same script? Im assuming this is a formatting issue I am running into?
Copy code
/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
/**
 * Module Description
 *
 * Version    Date            Author           Remarks
 * 1.00       Jan 12th, 2022    Fourth Wave Consulting
 *
 *  Name of Script	FWC-PO-Expected-Date.js
 *
 * Date			01/12/21
 *
 * Version		1.0
 *
 * Type			UserEventScript
 *
 *
 *
 *
 *
 * NetSuite Ver.	2017.2 or later
 *
 * Script record:
 * Deployment:
 * Primary function: afterSubmit
 *
 *
 * License 		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 *			EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *			MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 *			THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *			SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 *			OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *			HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 *			TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *			SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

/**
 * @param {String} type Context Types: scheduled, ondemand, userinterface, aborted, skipped
 * @returns {Void}
 */
define(["N/record"], function (record) {
  function afterSubmit(context) {
    // - get the value of {expectedreceiptdate} for all items and put into array
    // - If no items have `expectedreceiptdate` - end script
    // - Open 'created from' sales order and update the 'expected ship date' coulmn field with ID: {expectedshipdate} for each item to match the value from the PO.

    if (context.type == context.UserEventType.EDIT) {

      var oldRec = context.oldRecord;
      switch (context.type) {
        case context.UserEventType.CREATE:
          return;
        case context.UserEventType.EDIT:
          var itemLength = oldRec.getLineCount({ sublistId: "item" });
          log.debug({
            title: "Num Lines:  ",
            details: itemLength,
          });
          var items = [];
          if (itemLength === 0) throw "Order does not have any valid item";
          for (var index = 0; index < itemLength; index++) {
            var item = {};
            var itemId = oldRec.getSublistValue({
              sublistId: "item",
              fieldId: "item",
              line: index,
            });

            try {
              var itemRecord = record.load({
                type: record.Type.SERIALIZED_INVENTORY_ITEM,
                id: itemId,
              });
            } catch (ex) {
              if (JSON.parse(ex).name == "SSS_RECORD_TYPE_MISMATCH") {
                itemRecord = record.load({
                  type: record.Type.KIT_ITEM,
                  id: itemId,
                });
              }
            }
          }
          if (!itemRecord) throw ('Item with id ' + itemId + ' does not exist');
          item.expectedreceiptdate = itemRecord.getValue('expectedreceiptdate');
          log.debug({
            title: "Expected Receipt:  ",
            details: item.expectedreceiptdate,
          });
          break;
        default:
          throw "Invalid event type";
      }
    }
  }

  return {
    afterSubmit: afterSubmit,
  };
});
s
Why are you loading item records to get expected receipt date off the transaction? The expected receipt date should be sitting there the same way you get the item
To answer your original question,
define(['N/record', 'N/format'], function (record, format)
k
When I do the above, it tells me to use
require
instead, but when I change it to require it fails.
The expected receipt date is a field in each item row?
message has been deleted
I believe this is the correct logic
s
It looks to me like Sandii’s observation is correct…. it looks like you’re loading the ITEM record and which is not the transactional data you are looking for. The same result you would get going to List --> Accounting --> Items. What you looking for is the Item sublist value. There is no need to load the item record as the value you are looking for is not there. I think if you change your oldRec.getSublistValue to something like below then most everything after it is not needed.
Copy code
for (var index = 0; index < itemLength; index++) {
            var item = {};
            var line_expectedreceiptdate = oldRec.getSublistValue({
              sublistId: "item",
              fieldId: "expectedreceiptdate",
              line: index,
            });
k
Ok I got that all working now. How would I select a sublist line by the item id rather than line number? I am trying to sync items between the Purchase Order and Sales Order.
s
you would need to loop though the lines inspecting the itemId until you find the one you are looking for I think. OR run a query for the SO number and ItemID and get the line id from that and then select that line ID.