HOW TO RETURN A VALUE FROM A BACKEND SUITELET TO A CLIENT SCRIPT?

The following example uses a suitelet that run a search to identify the address corresponding to a location.
 Client Script
 In the client script entry point write the following code
        try {

            let suiteletURL = url.resolveScript({ //mention the suitelet script id's
                scriptId: 'scriptIdofSuitelet',
                deploymentId: 'deploymentIdofSuitelet',
                returnExternalUrl: false,
                params: {
                    'location': location,

                }
            });
             https.get.promise({
                url: suiteletURL//+ '&location=' + location
            }).then(function (response) {
                
                newShipAddress = JSON.parse(response.body);
                log.debug("newShipAddress",newShipAddress);
                
            }).catch(function (reason) {
                log.error("failed to get Location address", reason);
            });

        } catch (error) {

            console.log("ERROR@getState", error);
            return {};
        }


Suitelet script
function onRequest(context) {
         let request = context.request;
         let response = context.response;
         let address={};
         try {
             if (request.method === "GET") {

                let location = context.request.parameters.location;
                address = getAddress(location);// getAddress function runs a location search that returns the corresponding address value in JSON
               
                 response.write(JSON.stringify(address)); 
             }
             else {
                 response.write(JSON.stringify(address));
             }
         } catch (ERROR) {
     
             log.error("error@onrequest",ERROR);
             response.write(JSON.stringify(address));
         }
     }

Leave a comment

Your email address will not be published. Required fields are marked *