Good Morning, I am trying an https request for th...
# suitescript
k
Good Morning, I am trying an https request for the first time with a button click on a client script and I am not getting any response at all. I can't console.log or log.debug anything. Any advice on why?
Copy code
try{


        var apiUrl = "<https://clientapiqa.brandmanager360.com/>"


        const objData = {
            username: "netsuite api_12034",
            password: " ",
            grantType: "password"
        };


        var response = https.get({
            url: apiUrl + "/Artwork/DownloadFileById?fileId=43A50347-E30D-410C-952C-5928766B5643"
        })

        log.debug("Reponse" , response)

        let loginResponse = <http://https.post|https.post>({
                body: objData,
                headers: [],
                url: apiUrl
        });

    }catch (e) {
        log.debug("Error", e)
    }

}
c
So it's not executing at all? (stick an alert() at the top if you want to make sure)
How have you linked the function to the button?
k
Copy code
form.clientScriptModulePath = "SuiteScripts/CLCIntegration/ArtApprovalSubmitCScript.js"


form.addButton({
    id: 'custpage_CLCArtApprovalSubmit',
    label: 'Send Approval',
    functionName: `artApprovalCLCSubmit(${dataObj})`

})
c
What are you expecting that parameter to do?
What is the parmater, and where does the data come from?
k
The parameter just send a data object that I am later going to send in a post request
Its just a data object
c
Think how that would expand
Is there a reason why you can't get the data in the client script?
k
The first thing I want to do is send a login request that responds back with a bearer token
No I got the data is the client script
I am worried about the https.get and https.post request are not firing
I am not getting any response
c
I am saying the function is not even executing because of the
functionName
parameter
k
I have it in the correct function name
I just didn't post the whole function
c
My concern was how this expands: ` functionName: `artApprovalCLCSubmit(${dataObj})``
k
im confuse on what you mean by expands
c
Run this in your browser console: let dataObj = '345345()))3456387546'; let functionName =
artApprovalCLCSubmit(${dataObj})
console.log(functionName)
b
start with a button without parameters, then graduate to learning parameters
which typically requires an understanding of eval and generating strings that are parsed as script
k
@battk I am not looking for help with passing variables into functions I am looking for help with a https.post reques
b
your attempt at adding a parameter is bad enough to invalidate all the code in the button function
k
@battk How? when the function worked perfectly fine the way I administered the data. So its not a questions one whether my parameter placement is good enough? The questions is do you know how to use the https module? If you don't your concern should not be with how I decided to write the code that works
b
same answer as the previous person, log what you are passing to functionName
do it serverside, in the beforeLoad entry point
c
I don't think you're listening to what we're saying. Your concern was the logging is not working. We are saying the logging is not working because the function is not actually executing. This is not a "how does N/https work?" thing, that's another matter entirely
k
I am saying that my function is firing no problem.
b
what does the code look like now, the error you shared doesnt look like it can come from what you shared before
k
Copy code
function loginRequest() {


        var apiUrl = "<https://clientapiqa.brandmanager360.com/>"

        var objData = {
            "username": "netsuite api_12034",
            "password": "--password--",
            "grant_type": "password"
        };
        let loginResponse = <http://https.post|https.post>({
            url: apiUrl+"Token",
            headers:{"Content-Type" : "application/x-www-form-urlencoded",
                "Cache-Control" : "no-cache"},
            body: objData
        });

        console.log(loginResponse)

}
I took your advice and made it a function without a parameter
b
its not the client script where you were having problems
it what the user event script
k
???
b
the way you were writing the userevent script made the code you wrote in the client script irrelevant
k
o ok, cool but now i have a function on a client script. Can you help me?
b
the client script works perfectly fine, which means that the problem is in the user event script
k
how?
b
did you finish logging the functionName from your previous attempt
k
yes
b
what was it
k
loginRequest
b
the previous attempt, its so you understand why the code matters
the new error message you added suggests that you havent added N/https as a dependency in your client script
k
The documentation says that you can add N/https to client scripts
b
correct, but the error suggested you didnt
before it was get of undefined, which didnt make sense for the code you shared
the newest one is post, which does make sense if you didnt correctly add N/https as a dependency
c
I'd feel bad if I didn't also point out that it's very bad practice doing this with password shared in the client
m
I'd feel bad if I didn't also point out that it's very bad practice doing this with password shared in the client
And on a public Slack group 🙈