Hey! What would be the best approach in fetching ...
# suitescript
n
Hey! What would be the best approach in fetching data from an external resource, to a Suitelet custom form? I need to fetch data when loading it, but also dynamically after some user actions in the form. Fetching it in the Suitelet before rendering the form gives CORS policy issues, putting the fetch logic into the backend part of the Suitelet will give me the issue of calling that from my front-end part. What’s the best solution for doing this without unsecure/bad practice hacks?
r
Jumping in on this because I had the CORS policy issue too - interested to see potential solutions
👍 1
d
In the first instance, I'd look at using a Restlet as a proxy to fetch the data.
b
Use N/https clientside
Alternatively make your own suitelet to get the data and fetch it from your client side cide
n
@Dominic B @battk I’ve considered both options, will most likely go the the Suitelet as I already have one, thanks for the input guys
@battk I used N/https in a client script pageInit function, called my Suitelet, which in return calls a Restlet on another NetSuite account, that RL returns data to my Suitelet which then returns it to my Client script. But I still get CORS policy issues, any idea? (Notice that the data is fetched, but I still get the issue, so I don’t really get what’s happening)
b
what does the code look like
n
@battk CS:
Copy code
const suiteLetUrl = url.resolveScript({
                scriptId: 'customscript_***_sl',
                deploymentId: 'customdeploy_***_sl',
                returnExternalUrl: false
            });

var headerObj = {
    name: 'Content-Type',
    value: 'application/json'
};

return <http://https.post|https.post>({
    url: suiteLetUrl,
    body: {
        action: 'getCustomerCases'
    },
    headers: headerObj
}).body;
Suitelet:
Copy code
var url = '<restlet url>';
                        var method = 'GET';
                        var headers = oauth.getHeaders({
                            url: url,
                            method: method,
                            tokenKey: secret.token.public,
                            tokenSecret: secret.token.secret
                        });

                        headers['Content-Type'] = 'application/json';

                         return https.get({
                            url: url,
                            headers: headers
                        }).body;
The request type shouldn’t matter right? I mean the fact that I do a GET to the RL. Thinking about trying to do it as a POST instead, not sure why it would make a difference though..
b
which lines is the cors issue coming from
n
@battk trying to figure it out right now but it’s hard
@battk I debugged it a bit more, it seems I solved it by removing a
Copy code
{
    name: 'Content-Type',
    value: 'application/json'
}
Header from the call that the CS makes to the SL.. I’m not sure it’s resolved as it didn’t have a super clear pattern, sometimes it takes some time to show, but it’s take longer now than before and still don’t get it so hopefully that was it.. any ideas as to why that would create the problem? Anyways I’m just glad it seems to be working 🙂
b
sounds too weird
n
@battk it was haha.. it’s back, continuing the debugging
@battk ok so here’s a great story of how I wasted two days of my life: The data that I got back from the RL, and in turn from the SL to the CS, contained a HTML page:
Copy code
<HTML><HEAD><BASE href='https://*****.app.netsuite.com/'><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></HEAD><BODY style=\"font-family:Verdana,Arial,Helvetica,sans-serif;font-size:10pt;\"></BODY></HTML>
This was of course then trying to load as HTML in my Suitelet because this is part of the data I added to the HTML body of the Suitelet with my Client script, so it then tried to call the URL which, to confuse me even more is the same domain as the RL I got the data from. Happy days! Well now I know what the issue was, thanks for helping out, appreciate it!