We have added a button on a sutelet which we want ...
# suitescript
s
We have added a button on a sutelet which we want to trigger a post request to an external server with a payload
if (!netsuite_status) { form.addButton({ id: "skip_sync_btn", label: "Skip Sync", functionName:
handleButtonAction('${JSON.stringify(payload)}')
, }); } function handleButtonAction(dataStr) { //this is a client script }
1. should we post to a reslet which will post to the external system , 2. use a suitlet 3. use fetch and trigger clinet side
a
not sure if there's a best practice here, just a bunch of tradeoffs 🙂
logging is better server side so handling it there will give you that. but posting back to NS server to then post to external server is just an extra step for no real reason
s
i will have to expose the api keys to exteranl script
a
that was gonna be my next question, does the external server require auth, if so then I'd do it serverside rather than client side that would trump other considerations
b
your external server is likely badly designed if client side is an option. They should have cors restrictions
s
good point ,cors would probally block it
a
i mean i assume they've setup their server how they want it, if they want to allow CORS they can? but yeah if they don't then you'll need to do it serverside
s
What is the best way to handle it server-side, button to trigger client script
then sutielet or reslet (no UI needed)
a
is your button already on a suitelet form?
s
yes
a
if so then I'd have it POST back to the suitelet via a submit usually and handle it the POST section of the suitelet., but idk if you've got other stuff that needs to be handled via form submit (post) already?
but suitlet vs. restlet feels pretty inconsequential compared to serverside/clientsidte
s
The button is added from a POST response , so i don't want to do this on a sutilet , i wanted to keep the user on the page if i could
a
oooh ok yeah then url.resolve your other script and post to that 🙂
s
post to reslet or 2nd suitelet ?
a
I'm not sure it makes much difference tbh, obviously either can work
b
choose the suitelet if you think they are the same, restlets are forced to have an external url, suitelets are not
s
Copy code
const url = urlMod.resolveScript({
      scriptId: "customscript1824",
      deploymentId: "customdeploy1",
    });

    // Use N/https.post instead of fetch
    var response = https.post({
      url: url,
      body: JSON.stringify(dataStr),
      headers: { "Content-Type": "application/json" },
    });    this is my current code from the client script
sutielet would send the user to that URL (plus it has additional overhead?
b
using https.post (or fetch for that matter) does not change the url
s
i switched to Suitlet, am i having trouble making a post request
i can get it as a get and query parms, but i don't like that practice
b
my guess from the code you shared is that you are getting unexpected input since you stringified the data twice
s
@battk yes good catch
This is one of the downsides of Copilot auto correct