Home > Blockchain >  NetSuite sample from Help Center giving error, unable to run
NetSuite sample from Help Center giving error, unable to run

Time:12-17

I'm trying to learn just the bare basics about suitelets in my NetSuite environment. I have copy/pasted this code from the Help Center and I am receiving this error when clicking the URl "An unexpected error has occurred. Please click here to notify support and provide your contact information."

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */
define([], function() {
    function onRequest(context) {
        var html = '<html><body><h1>Hello World</h1></body></html>';
        context.response.write(html);
        context.response.setHeader({
            name: 'Custom-Header-Demo',
            value: 'Demo'
        });
    }
        
    return {
        onRequest: onRequest
    };
}); 
        
                

I have tried putting log.debugs within the script but I'm not getting anything in the execution log. I have uploaded the script, deployed, and released the suitelet but I'm still getting this error. Error Screenshot

CodePudding user response:

Tips For Success With Suitlets

1. Think like you're coding Node Express / Nextjs APIs

  • Set up your code to handle each request method type
/**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
define(["N/cache"], /**
 * @param {cache} cache
 */
(cache) => {
  /**
   * Defines the Suitelet script trigger point.
   * @param {Object} scriptContext
   * @param {ServerRequest} scriptContext.request - Incoming request
   * @param {ServerResponse} scriptContext.response - Suitelet response
   * @since 2015.2
   */

  const onRequest = (scriptContext) => {
    let sc = scriptContext;
    let req = sc.request;
    let res = sc.response;
    let { method, parameters, body, clientIpAddress, headers } = req;
    let { write, setHeader } = res;

    switch (method) {
      case "GET":
        // Run logic for each specific request type (Save creating files for each operation)

        // Set header first then return
        setHeader({
          name: "Content-Type",
          value: "application/json",
        });

        // return your server response
        write({
          output: "Hello World",
        });

        break;

      case "POST":
        // Add logic here for POST
        break;

      default:
        log.error({ title: `❌ method: ${method} not supported` });
    }
  };

  return { onRequest };
});

2. Utilize WebStorm & The SuiteCloud IDE Plugin

  • This will help you template out files faster and import modules and their JSDocs automatically along with giving you better IntelliSense. Along with instantly uploading your file instead of having to do it through the UI of NetSuite every time.

3. Use 2.1 Where its supported

  • Using 2.x is similar to ES2012 which is not the best for using let, const, and all the new ES6 functions natively available to us. Along with using string templating or literals as in the example above.

4. Use N/cache If possible

  • If you're fetching data like a record or ID to retrieve. Using N/cache will make the suitelet's execution much faster.

Related to your error

Try what I suggested in the code snippet. You want to set your header first before you return your response. As you're trying to set a header where the response has already been sent. Also NetSuite don't understand half the time the type of data you're sending/returning. So setting the Content-Type header will allow your response to be displayed as expected.


        // If you're trying to send JSON you stringify it first before you send the data
        write({
          output: JSON.stringify({
            text: 'Hello World'
           })
        });

If you are sending HTML as a response you would do the following

    
     // Set header first for content return define
     setHeader({
       name: "Content-Type",
       value: "text/html",
     });

     write({
       output: '<html><body><h1>Hello World</h1></body></html>'
     });

Hope this helps!

  • Related