I'm trying to read parameters from `context.reques...
# suitescript
e
I'm trying to read parameters from
context.request
in a UE
beforeLoad
, and I am getting an error
type":"error.SuiteScriptError","name":"TypeError","message":"Cannot read property \"getAllParameters\" from undefined"
. Should I take it as a fluke? I'm checking params as
if (context.request.parameters && context.request.parameters.projtask)
s
I've seen similar errors with SS2
I think beforeLoad sometimes has issues
i.e. - nowhere in your code are you invoking 'getAllParameters', right?
e
yeah, I don't call that myself
j
it probably has to do with netsuite implementing context.request.parameters as a getter
in other words, whenever you access context.request.parameters it actually executes a function and returns a value https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get
e
this just keeps getting better.
Copy code
{
  "type": "error.SuiteScriptError",
  "name": "TypeError",
  "message": "Cannot read property \"getMethod\" from undefined",
  "stack": [
    "createError(N/error)",
    "beforeLoad(/SuiteScripts/BMI_UE_BPMSTimeBill.js:123)",
    "createError(N/error)"
  ],
  "cause": {
    "message": "Cannot read property \"getMethod\" from undefined",
    "fileName": "/SuiteScripts/BMI_UE_BPMSTimeBill.js",
    "lineNumber": 123,
    "name": "TypeError",
    "stack": "\tat /SuiteScripts/BMI_UE_BPMSTimeBill.js:123 (beforeLoad)\n\tat INVOCATION_WRAPPER$sys:27\n\tat INVOCATION_WRAPPER$sys:19\n\tat INVOCATION_WRAPPER$sys:37\n\tat INVOCATION_WRAPPER$sys:1\n",
    "rhinoException": "org.mozilla.javascript.EcmaError: TypeError: Cannot read property \"getMethod\" from undefined (/SuiteScripts/BMI_UE_BPMSTimeBill.js#123)"
  },
  "notifyOff": false,
  "userFacing": true
}
that's for a log.debug call lol
s
that 'getMethod' I've seen before.
e
the log.debug is in a try/catch. I really don't want to swallow the catch if I don't have to but...
s
is it the TimeBill.js file that you're workign on?
e
yeah
s
I have that getmethod error happen when I enable NFT autologging and it's being invoked indirectly e.g. a Scheduled Script that loads a record... and hence a UE
beforeLoad
is fired
are you seeing this when directly loading a timebill record or is this record loaded indirectly by some other script?
e
I'm not sure, it's deployed to a client's account. I'm handling
CREATE
, I wonder if they're doing something I'm not aware of
s
I haven't discovered why it happens, but I'm pretty sure it's not NFT as the logger works reliably in all other cases. I suspect something about the
ctx
object provided by NS is wrong, at least when scripts are triggered by another script indirectly. I've never seen this issue on a direct use (e.g. beforeLoad() running when record is loaded via UI)
j
that's a good point the before load for a user doing someting in their browser would have a
request
the before load for the server creating/loading a record probably would not
e
agreed
s
I'd like to know what exactly is
undefined
in that error... maybe the ctx.request is actually undefined and it's a NS bug that's trying to access it?
e
Guess I'll log the executionContext
maybe hard if
N/log
isn't loaded through lol
j
It looks to me that the request.parameters property is implemented as a getter so it gets the parameters from some other internal object. Since getAllParameters() is a method on SS1
nlobjRequest
and since so much of SS2 is a wrapper around SS1 I imagine that when you do
ctx.request.paramters
it delegates it to
private_nlobjRequest.getAllParameters()
but in this case
private_nlobjRequest
is undefined/null
likewise getMethod
s
is that's true @jkabot then it's definitely a NS bug
who wants to take on filing a ticket? 🙂
e
I probably will, eventually
s
maybe if we all do it will get noticed?
j
I'm just speculating but that's what makes sense based on symptons and stack trace they probably forgot something like
Copy code
if (!private_nlobjRequest) {
  throw new Error('There is no request for this before load user event');
}
s
well, I would hope that it wouldn't throw exceptions
just that
request
is undefined if the script is serverside?
it shouldn't blow up inside
j
that would be most appropriate
s
I do need to put in a workaround for this in NFT so it's solved in just one place... I hadn't given it much thought till now and @jkabot I think your speculation is likely truth
the explanation sounds logical to me, anyway
j
I guess the way to avoid it might be to check the execution context using N/runtime
e
aye, that's what I am doing now, and checking context.request
j
or good ol 😅
Copy code
function hasRequest() {
  try {
    ctx.request.parameters; 
    return true;
  } catch(exc) {
    return false;
  }
}
s
it's a long held rule that property access should not throw
argh
e
should return undefined?
I think that's what was in the doc from mdn
s
perhaps, but I'm talking about general OO rule/best practice across multiple languages
j
One of my gripes with getter and setter properties That it "could" throw or have side effects is opaque to the user of the API
e
I come from c#, so js is a different beast
j
Another example in netsuite is the filterExpression property on the search object it's implemented as a getter/setter, something like
Copy code
get filterExpression() {
  return copyOfInternalArray();
}
I was trying to add some filters like
Copy code
sea.filterExpression.push('AND', ['internalid', 'anyof', [whatever]]);
but that was modifying the copy of the internal array that was passed to me
and having no effect on the actual filters of the search
e
that was yesterday or today wasn't it? I recall reading that thread
j
maybe within the last week? anyways the cause of that kind of behavior (my code not having an effect on the search) can be really tricky to tease out
e
not like you don't have enough to remember right? just another quirk
I thought UE's didn't trigger other UE's? The
runtime.executionContext
is
USEREVENT
...
j
🤔 that's confusing I did a quick search in suiteanswers and it says that execution context can be userevent for CSV Imports (as if that made sense) https://netsuite.custhelp.com/app/answers/detail/a_id/33441/kw/execution%20context
s
I do all my suitescript in TypeScript, so it's much closer to C# I suppose
e
I need to jump on your NFT. I haven't had time yet, but plan to. Are custom records easy to deal with?
s
Actually, if you have SDF working NFT can auto-generate the strong typed classes for custom records from the SDF xml. I hope to have auto code generation across all netsuite records as time allows. That's one thing I miss from the old SOAP web service days.
So to answer your question, custom records are actually the easiest thing to work with as far as NSDAL goes
e
Excellent - next project I'll give it a go