We cannot use server-side script methods to do operations in client script because it is a client-side script. Hence if we have to get some value from server-side methods, we can use the https.post.promise method.
https.post.promise allows client-side scripts to asynchronously send data to and receive responses from server-side scripts (like Suitelets).
Hence we can use the client script to send the request and handle the response, while a suitelet processes the POST request and returns the required data.
(NB: We have to add the https module for this function to be used.)
Example:
https.post.promise({
url: ‘suitelet url’,
headers: {
‘Content-Type’: ‘application/json’
},
body: JSON.stringify(params)
}).then(function (response) {
let responseBody = JSON.parse(response.body);
console.log(‘Response:’, responseBody);
if (responseBody.status === ‘success’ && responseBody.data) {
let redirectUrl = responseBody.data;
window.location.href = redirectUrl;
} else {
console.error(‘Unexpected response format:’, responseBody);
}
}).catch(function (error) {
console.error(‘Error:’, error);
});
Here we are using the function to create a URL in the post block of the suitelet. The URL is returned as the value of the ‘data’ key.
We use .then() to handle responses and .catch() to handle errors.