Anyone here with experience using `N/redirect`? I’...
# suitescript
a
Anyone here with experience using
N/redirect
? I’m trying to use it and, well, it’s not redirecting.
d
Can we take a look at the block of code with the redirect in it?
a
Sure. One sec
Copy code
const afterSubmit = scriptContext => {
  const inspectionId = 57144;
  log.debug( {
    title: "Redirecting to customrecord_inspection",
    details: inspectionId
  } );
  _Redirect.toRecord({
    type: "customrecord_inspection",
    id: inspectionId
  });
};
I know that the record with ID 57144 exists.
d
This is going to sound weird... but try stringifying the id maybe?
a
Hey, I’m up for anything at this point
No dice 😞
d
what does it do? Just return to the record you were saving?
a
I also tried using the integer version of the record ID and that straight up throws an error.
d
also... I assume that log is showing up fine?
a
So, this is a UE script for another custom record. When one of these is saved (in the future, when it’s deleted) I want to send the user back to Inspection record that it was associated with
But I can’t even get it to redirect in this simplified case
The log does indeed appear as expected.
d
It's just landing back on the original custom record that was being saved?
a
Yes, It’s going back to the previous page
If I get to this custom record by clicking a link, then it goes back to the page on which I clicked that link
d
what does the built version of this file look like?
everything looks fine to me
all I can think of now is something in the built process making it wrong
a
Hence why I’m so stumped…
d
what is the error you are getting?
d
no error, he's just getting landed back on the original page
a
Correct
I also just verified that if I go directly to this custom record and save it, it just goes to the view-mode version of this record instead of redirecting.
d
I'm also wondering if this has something do with it:
Copy code
synchronous afterSubmit user events
d
Copy code
_Redirect
are you sure on this _Redirect is referred to redirect modul parameter
a
@dmashburn3 I thought of that. Async only applies to Sales Order and Customer scripts, apparently, and is turned off within our account
d
Can I know where does this UE is applied ?
a
@D17339yes
message has been deleted
👍 1
d
Hail mary is all I have left... try putting it on another record somewhere else and/or delete deployment and redeploy
a
I’ll give it a try. So strange…
@D17339 It’s a custom record called “Inspection Defect”
d
I think the redirect module might be a little weird. We were using
redirect.toSearchResults
in a suitelet... which wasn't supported, but worked... except for that it duplicated columns
So it definitely might not be a fully fleshed out module in some ways
a
Could be. That’d be unfortunate.
d
can you share the complete complete script if possibl?
a
Sure
message has been deleted
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 *
 * @typedef {Object} log
 * @typedef {Object} util
 *
 * Author: aaronkoenes
 * Date Created: 2020-01-15
 */
define( [
      "N/search",
      "../Custom Modules/TSS_CM_Extensions",
      "./TSS_CM_InspectionDefect",
      "../Inspections/TSS_CM_Inspections",
      "N/record",
      "N/runtime",
      "N/redirect"
    ],

    ( _Search, _Ext, _InspectionDefect, _Inspections, _Record, _Runtime, _Redirect ) => {

      /**
       * Function definition to be triggered before record is loaded.
       *
       * @param {Object} scriptContext
       * @param {Record} scriptContext.newRecord - New record
       * @param {string} scriptContext.type - Trigger type
       * @param {Form} scriptContext.form - Current form
       * @Since 2015.2
       */
      const beforeLoad = scriptContext => {
        // disabled in return object
      };

      /**
       * Function definition to be triggered before record is submitted.
       *
       * @param {Object} scriptContext
       * @param {Record} scriptContext.newRecord - New record
       * @param {Record} scriptContext.oldRecord - Old record
       * @param {string} scriptContext.type - Trigger type
       * @Since 2015.2
       */
      const beforeSubmit = scriptContext => {
        let newRec = scriptContext.newRecord;
        const isDelete = scriptContext.type === scriptContext.UserEventType.DELETE;
        const isCreate = scriptContext.type === scriptContext.UserEventType.CREATE;
        const isXedit = scriptContext.type === scriptContext.UserEventType.XEDIT;

        /*  Update the AQL if:
         *    1) We're creating a new Inspection Defect or
         *    2) We're editing the defect field on an exising Inspection Defect
         *
         *  If we're deleting an inspection defect, don't perform this check or
         *  else scriptContext.oldRecord will throw an error.
         */
        let updateAQL = isCreate;
        if( !updateAQL && !isDelete ) {
          const oldDefect = scriptContext.oldRecord.getValue({
            fieldId: "custrecord_inspdef_defect"
          });
          const newDefect = scriptContext.newRecord.getValue({
            fieldId: "custrecord_inspdef_defect"
          });
          updateAQL = oldDefect !== newDefect;
        }

        // If this is an XEDIT, load the Inspection Defect record in full.
        // This record can only be used to GET data, not to set data that needs
        // to be saved once beforeSubmit completes
        if( isXedit || isDelete ) {
          newRec = _Record.load({
            type: _InspectionDefect.RecordId,
            id: newRec.id
          });
        }

        /* Check the status of the Inspection associated with this record. If
         * it's not in an allowed state, don't save this entry
         */
        const inspectionId = newRec.getValue({
          fieldId: "custrecord_inspdef_inspection"
        });

        if( !inspectionId ) {
          throw "No inspection was specified";
        }

        const inspectionStatus = _Search.lookupFields({
          type: _Inspections.RecordId,
          id: inspectionId,
          columns: ["custrecord_insp_status"]
        }).custrecord_insp_status;

        if( inspectionStatus ) {
          const status = inspectionStatus[0].value;
          const disallowedInspectionStatuses = [
            _Inspections.Status.PAUSED,
            _Inspections.Status.CANCELED,
            _Inspections.Status.PASSED,
            _Inspections.Status.FAILED
          ];
          const editAnytimeUsers = [
              _Ext.USERS.AARON_KOENES,
              _Ext.USERS.CHRIS_GEARINGER
          ];

          if( _Runtime.getCurrentUser().id.isNoneOf( editAnytimeUsers ) ) {
            if( status.isAnyOf( disallowedInspectionStatuses ) ) {
              throw "You cannot record a new defect for an " +
                "inspection that isn't in progress";
            }
          }
        }

        if( updateAQL ) {
          const defectID =
              newRec.getValue({ fieldId: "custrecord_inspdef_defect" });
          const defectAllowedQualityLimit = _InspectionDefect.getAQL( defectID );

          scriptContext.newRecord.setValue({
            fieldId: "custrecord_inspdef_aql",
            value: defectAllowedQualityLimit
          });
        }
      };

      /**
       * Function definition to be triggered after record is submitted.
       *
       * @param {Object} scriptContext
       * @param {Record} scriptContext.newRecord - New record
       * @param {Record} scriptContext.oldRecord - Old record
       * @param {string} scriptContext.type - Trigger type
       * @Since 2015.2
       */
      const afterSubmit = scriptContext => {
        const inspectionId = "57144";
        log.debug( {
          title: "Redirecting to customrecord_inspection",
          details: inspectionId
        } );
        _Redirect.toRecord({
          type: "customrecord_inspection",
          id: inspectionId
        });
      };

      return {
        // beforeLoad: beforeLoad,
        beforeSubmit: beforeSubmit,
        afterSubmit: afterSubmit
      };

    } );
d
I am sorry for asking this customrecord_inspection are you sure this is your exact record type and respective id
because i see you are using this in BS _InspectionDefect.RecordId
a
A valid question. Yes, it’s the correct type. Inspections and Inspection Defects are two different record types
message has been deleted
👍 1
message has been deleted
👍 1
b
are you the one trying to redirect on delete?
a
And the variable storing the record ID resolves to
Copy code
customrecord_inspection
@battk yes
b
i believe your answer is netsuite wont let you
a
Well, at the moment, it’s not letting me redirect at all, for anything
Even when I just edit
b
switch to suitescript 2.0
netsuite 1
😀 1
a
I’ll try it a sec
That… worked…
🔥 1
Seriously?!
d
go figure
and here I was thinking you were being pretty fancy using that 2.1
👍 1
b
make sure to tell netsuite support
i salute the brave guinea pig
a
@dmashburn3 tcha, me too.
Well, I can’t pull any of my 2.1 custom modules into a 2.0 script, so it looks like I won’t be using this for now.
@battk Thanks for figuring that out for me!
b
standard advice, dont use stuff labeled beta
unless you know how it works normally