I have a function `foo()` being defined outside of...
# suitescript
r
I have a function
foo()
being defined outside of a
define()
statement as part of client scripts loaded in EDIT mode so that it can be called via a button created with
form.addButtion()
defined via a User Event script. Within that function
foo()
, I'm using
require()
to load necessary modules to do what the function needs to do on the client side. The issue is how to deal with custom modules. All of the custom modules I'm using use a path reference that is defined in an
namdconfig.json
, so for a define statement, it'd be
define(['N/https', 'ME/mine'], (https,mine) => {...})
, however when trying to do the same with require
require(['N/https', 'ME/mine
], (https,mine) => {...}` , the
mine
module isn't loaded. Doing some research, if I configure the require function directly before use the following inside of
foo()
Copy code
let runRequire = require.config(<contents of namdconfig.json>);
runRequire(['N/https', 'ME/mine'], (https/mine) => {...});
then the
mine
module is available for use. The issue with that is having to copy the contents of
namdconfig.json
into each function that needs it. I'd prefer to just point to the file, but
require.config()
needs an object. So is there any reasonable way to use
require()
in a client script with custom modules in a sane way?
e
Can you help me understand why
foo()
is defined outside the Client Script's
define()
? I'd expect it to be defined within the
define()
and included in the Client Script's
return
statement so that it would be accessible to the button. Then as long as the Client Script has
@NAmdConfig
defined, I'd expect the custom module to be found. I haven't tried this specifically with the inline
require()
call though.
r
appreciate the help. So I'm trying to be aware of the "only 10 deployed scripts are executed" rule as I've run into it before. to curb that I have a
cs_sales_orders.js
client script attached to all sales orders.
cs_quote.js
client script attached to quotes. Both of those scripts use a variation of
define([...,'ME/common'], (..., meCommon) => { ... }
to get the common client functions that will run for both transactions.
pageInit(), fieldChanged()
, etc.. all run on the
cs_*.js
files, and then call
meCommon.otherFunc()
when using something in common. I tried defining the
foo()
function (and exporting) the button calls within the
ME/common
module, but when the button is clicked, says function is not defined. My guess is that because it's defined within
ME/common
and not on the original deployed client script, it's not visible.
(I'll also take "what are you doing?!" as an answer if I should be approaching having multiple deployments scripts use common custom module libraries a different way. It's worked great up until I need to have a button defined in a user Event call a client side function)
😂 1
a
How is
meCommon
structured? are you sure you are exporting/returning
foo()
?
a
I think the part we're all struggling with is this initial premise
I have a function
foo()
being defined outside of a
define()
statement as part of client scripts loaded in EDIT mode so that it can be called via a button created with
form.addButtion()
defined via a User Event script.
a function called by a button click on form that was created in a before load UE doesn't NEED to be outside of the define
oh wait, edit mode hmm 😕
hmm can you not just setup a passthru function that is an actual named function in the client script, but all THAT does is call the common function in the custom module?
Copy code
function buttonClicked({params}){
    meCommon.actualFunction({params});
}
or are you saying that effectively what you did with your
foo()
?
r
thank you everyone for the help. Based on the discussion, I came up with a solution that will work and be able to use custom modules within a
define()
, and am no longer using
require()
or defining any functions outside of a
define()
statement. @battkI have put my own Jira a revisit to look at consolidating within a single
.clientScriptModulePath
file in the future.