Peoples, I am having some troubles getting a User...
# suitescript
k
Peoples, I am having some troubles getting a User Event script to run correctly on AfterSubmit. I am trying to: 1. get the value of
{expectedreceiptdate}
for all items and put into array
2. Open ‘created from’ sales order and update the ‘expected ship date’ column field with ID:
{expectedshipdate}
for each item to match the value from the PO.
However I cannot get anything to show up in the console. I have also tried to log this value to no avail. It does run correctly but all I see in the Execution Log is “Start Script”. What am I doing wrong?
n
Userevent logs cannot be printed in console.
👍 1
Also share the code.
k
function
_afterSubmit_(context) {
_log_._debug_('Start Script ');
// - 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_._CREATE_
&&
_context_._type_
!==
_context_._UserEventType_._DELETE_ ) {
_return_;
}
// Get number of Item lines from Purchase Order Record
_var_ CurrentRecord _= context.currentRecord_;
_var_ numLines _= CurrentRecord.getLineCount({_"sublistId"_:_ "item"_})_;
_log_._debug_('Lines: ', numLines);
}
Makes sense, I tried to change it to a debug statement but still same result.
n
_var_ CurrentRecord _= context.currentRecord_;
This is not a valid statement in UE.
You will need to load the record.
context.newRecord.id will give you the ID of the record.
Use it to load the record and then set the values.
then save it.
👍 1
k
Copy code
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 objRecord = record.load({
      //     type: record.Type.PURCHASE_ORDER,
      //     id: context.newRecord.getValue({fieldId: 'transaction'}),
      //     isDynamic: true
      //   });
      var oldRec = context.oldRecord;
      switch (context.type) {
        case context.UserEventType.CREATE:
          return;
        case context.UserEventType.EDIT:
          //   var payload = getSalesOrderItems(newRecord);
          //   log.debug(payload);
          //   var newRecord = context.newRecord
          // Get number of Item lines from Purchase Order Record
          //var CurrentRecord = JSON.stringify(context.currentRecord);
          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";
      }
    }
I have that now, but the date field is returning blank….
n
item record doesn't have expected receipt date field. This field is on the item sublist on the purchase orders.
You will need to get this value from the PO lines, not from the items.
k
Ahhh, that makes sense. Ok now that is working
thank you
👍 1