Has anyone got a suitelet, launched from a button ...
# suitescript
s
Has anyone got a suitelet, launched from a button on a transaction, to post information back to that transaction? I need a way to select items in the suitelet, then on post update the transaction. The transaction will not be saved, so I cant do a record.update. I have seen this done before, but cant find any examples.
c
You can get a handle to what called the suitelet (the transaction record) by using window.opener
p
I made one that uses datatables and posts the IDs back using browser local storage which a client script then picks up and deals with
s
Im confused about this statement
The transaction will not be saved, so I cant do a record.update.
, you are saying you want to do the edits in the suitelet, and have it go back to the transaction in edit mode with the changes you made from the suitelet, maybe?
s
@creece @PNJ - thanks I will check those methods out. @Sandii - correct, this will be a new transaction. trying to emulate the "Add Multiple" button basically, which would be used on mainly new, unsaved trans
e
The window.opener approach is what you need (suitelet + client script), you will be able to call the suitescript API in the context of the record instead of the suitelet.
s
Ok - so i tried window.opener but I cant get it to work - does anyone have any sample code so I can see how they interact? I have the button launching the suitelet, i then tried to define some code based on suiteanswers https://netsuite.custhelp.com/app/answers/detail/a_id/88583/kw/window.opener to use a module but I cant seem to get it working. Any pointers massively appreciated! 😂
I presumed the require statement would bring the client script into the transaction for use, but it doesnt seem to do that
c
Copy code
function addSelectedItems(dataIn) {

      var itemSelectionRecord = dataIn.itemSelectionRecord;

      const SELECTED_ITEMS = getSelectedItems({
          itemSelectionRecord: itemSelectionRecord
      });

      const PRIMARY_SYSTEM_CODE_ROLE_ID = itemSelectionRecord.getValue({
          fieldId: PCG_OEW_CONSTANTS.ITEM_SELECTION_FORM.HEADER.FIELD.PRIMARY_SYSTEM_CODE_ID.id
      });

      if (!SELECTED_ITEMS.length) {
          dialog.alert({
              title: PCG_OEW_CONSTANTS.DIALOG.NO_ITEM_SELECTED.title,
              message: PCG_OEW_CONSTANTS.DIALOG.NO_ITEM_SELECTED.message
          }).then().catch();
          return;
      }

      window.onbeforeunload = function() {}; // Get rid of the "are you sure..." popup when navigating away

      window.opener.require(['N/currentRecord'], function(currentRecord) {

          var currentTransaction = currentRecord.get();

          for (var i = 0; i < SELECTED_ITEMS.length; i++) {

              currentTransaction.selectNewLine({
                  sublistId: PCG_OEW_CONSTANTS.RECORD.TRANSACTION.SUBLIST.ITEM.ID
              });

              currentTransaction.setCurrentSublistValue({
                  sublistId: PCG_OEW_CONSTANTS.RECORD.TRANSACTION.SUBLIST.ITEM.ID,
                  fieldId: PCG_OEW_CONSTANTS.RECORD.TRANSACTION.SUBLIST.ITEM.FIELD.ITEM.id,
                  value: SELECTED_ITEMS[i].id,
                  forceSyncSourcing: true
              });

              currentTransaction.setCurrentSublistValue({
                  sublistId: PCG_OEW_CONSTANTS.RECORD.TRANSACTION.SUBLIST.ITEM.ID,
                  fieldId: PCG_OEW_CONSTANTS.RECORD.TRANSACTION.SUBLIST.ITEM.FIELD.PRIMARY_SYSTEM_CODE.id,
                  value: PRIMARY_SYSTEM_CODE_ROLE_ID,
                  forceSyncSourcing: true
              })

              currentTransaction.commitLine({
                  sublistId: PCG_OEW_CONSTANTS.RECORD.TRANSACTION.SUBLIST.ITEM.ID
              });
          }

          window.close();
      });
  }
^ This function is called on a suitelet popup button click.
You need to leave the forceSyncSourcing to true as well or its not going to work as expected.
s
@creece wow, thank you so much for this - got it working!! I notice you made the Webstorm Netsuite plugin too (well it looks like you did!) which is amazing - smashing it on all fronts! 😆
keanu thanks 1
@eminero thanks for your help too
💪 2