Hello All, Is there a way that I can reload a page...
# suitescript
c
Hello All, Is there a way that I can reload a page after downloading the PDF using context.response.writeFile from suitelet. I’m in the POST function. Thanks in advance!
r
Can I assume you're using a client script to call the suitelet? By writing to the response, you're releasing execution on the Suitelet. Use
redirect
from the caller.
c
Thanks @reptar for the response. I have a custom button in the Suitelet page. When user clicks It then I’m triggering the submit event by jQuery[type = “ submit”] to call the suitelet POST method on demand so the response won’t come back.
r
are you using
form.addButton()
? If so, add a client script to your suitelet form. Use the CL for button handling. Call to SL post with
https.post.promise()
, then use
redirect
.
c
Thanks @reptar. I have followed this approach before. The problem here is the front end page is blocked for a longtime until the response get back from server.
@battk any thoughts here? Thankyou!
b
what did your attempt look like
c
@battk I’m in the suitelet POST method and my logic is quite straightforward which is downloading the PDF to the local system using this.context.writeFile(fileObj). After this, I need to refresh the current suitelet page once the pdf is downloaded. Is there a way that I can do that? I tried multiple ways to achieve it but since I’m in POST method not sure If that works.
The overview of this is, I have a custom button on suitelet page, once user clicks on this button I’m calling suitelet POST method on demand from the client using jQuery(“[type= ‘submit’]”) and there is a custom field on the suitelet form which sets the value of the custom button element name. In the suitelet POST method, I’m reading this value from the form field and doing necessary actions in the POST methods. I have followed this approach because I have multiple custom buttons and these buttons should act as standard submit button.
b
do what reptar suggested, use https.post.promise in the client script
alternatively fetch, which is easier to use with FormData if you dont want to change how your suitelet post works
1
after that you can use createObjectURL to create a url that you can then open
🤔 1
if you are willing to change the suitelet, you can make a GET on the suitelet return the file, which allows you to use open directly
c
I have used https.post.promise() here but then when I use context.response.writeFile(fileObj) in post method of suitelet, the problem I’m facing is Instead of downloading the file It is returning the file object string as a response back. Any Idea @battk
Below is the snippet I'm using in the post method of suitelet.
let pdfFile = render.xmlToPdf({
xmlString: xml
});
let result = pdfFile.getContents();
let fileObj = file.create({
name:"printReview.pdf",
fileType:file.Type.PDF,
contents:result
})
this.context.response.addHeader({
name:"Content-Type",
value:"application/pdf"
})
this.context.response.writeFile({
file:fileObj
});
Copy code
https.post.promise({
    url: baseUrl,
    body: JSON.stringify(pdfData),
    headers: headerObj
})
    .then(function(response){
        log.debug({
            title: 'Response',
            details: response
        });
    })
    .catch(function onRejected(reason) {
        log.debug({
            title: 'Invalid Request: ',
            details: reason
        });
    })
132 Views