Latif
Latif

Reputation: 17

`Promise is not defined`, when trying to hit endpoint with https module

I'm trying to make sure that any changes on salesOrder on Netsuite get reflected on my salesOrder collection copy in a Cloud Firestore Database.

For some reason the response that I get when trying to edit and save a salesOrder is this org.mozilla.javascript.EcmaError: ReferenceError: "Promise" is not defined. (/SuiteScripts/postSalesOrder-v2.js#30)

This is the script that is linked to a salesOrder:

/**
 * User Event 2.0 example detailing usage of the Submit events
 *
  @NApiVersion 2.x
  @NModuleScope SameAccount
  @NScriptType UserEventScript
  @appliedtorecord salesorder
 */

define(['N/https'], function(https) {
  function myAfterSubmit(context) {
    var apiURL = 'https://myApiEndpoint';
    var headers = {
      'content-type': 'application/json',
      accept: 'application/json'
    };

    https.post
      .promise({
        url: apiURL,
        headers: headers,
        body: JSON.stringify(context.newRecord)
      })
      .then(function(response) {
        log.debug({
          title: 'Response',
          details: response
        });
      })
      .catch(function onRejected(reason) {
        log.debug({
          title: 'Invalid Post Request: ',
          details: reason
        });
      });
    return true;
  }

  return {
    afterSubmit: myAfterSubmit
  };
});

Upvotes: 0

Views: 1414

Answers (2)

Ronnie Overby
Ronnie Overby

Reputation: 46470

These days it is possible to use http.[get|post|etc].promise in server-side SuiteScript.

It's anyones guess whether or not anything actually runs in parallel if you invoke several of them or whether the script will exit without promise fulfillment, but the API does work as of SuiteScript 2.1.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/https'], function (https) {
    function afterSubmit(context) {        
        https.post.promise({ 
            url: "https://stackoverflow.com",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({
                message: "Hey planet"
            })
        });
    }

    return {
        afterSubmit: afterSubmit
    };
});

That tag in the comment (@NApiVersion 2.1) is important to remember or else you are using SuiteScript 2.0, which doesn't support newer Javascript features.

Upvotes: 0

bknights
bknights

Reputation: 15402

Server side http calls are synchronous and return the response rather than a Promise.

Upvotes: 4

Related Questions