SuiteScript 2 Http 请求与回调

SuiteScript 2 Http requests with Call Back

您好,我需要与外部设备交互以通过 http 传输数据。我知道 SuiteScript 1 有一些限制,但是 SuiteScript 2 呢?有没有办法在 2.0 中发出带有负载的 HTTP 请求并回调 提前感谢您的帮助

您需要查看 N/httpN/https 模块。每个都为典型的 HTTP 请求类型提供方法,并且每个请求类型都有一个 API,returns 承诺用于您的回调实现。

来自 NS 帮助的非常简单的示例:

http.get.promise({
    url: 'http://www.google.com'
})
.then(function(response){
    log.debug({
        title: 'Response',
        details: response
    });
})
.catch(function onRejected(reason) {
    log.debug({
        title: 'Invalid Get Request: ',
        details: reason
    });
})

这是我拥有的一个非常基本的(减去负载中的许多额外字段),我用它来将 NetSuite 项目发送到 Salesforce,然后使用响应中的 Salesforce ID 更新 NetSuite 项目.这是您要找的吗?

define(['N/record','N/https'],function(record,https){
  function sendProductData(context){
    var prodNewRecord=context.newRecord;
    var internalID=prodNewRecord.id;
    var productCode=prodNewRecord.getValue('itemid');
    var postData={"internalID":internalID,"productCode":productCode};
    postData=JSON.stringify(postData);
    var header=[];
    header['Content-Type']='application/json';
    var apiURL='https://OurAPIURL';
    try{
      var response=https.post({
        url:apiURL,
        headers:header,
        body:postData
      });
      var newSFID=response.body;
      newSFID=newSFID.replace('\n','');
    }catch(er02){
      log.error('ERROR',JSON.stringify(er02));
    }

    if(newSFID!=''){
      try{
        var prodRec=record.submitFields({
          type:recordType,
          id:internalID,
          values:{'custitem_sf_id':newSFID,'externalid':newSFID},
        });
      }catch(er03){
        log.error('ERROR[er03]',JSON.stringify(er03));
      }
    }
  }

  return{
    afterSubmit:sendProductData
  }
});

*注意:正如@erictgrubaugh 提到的那样,承诺将是一个更具可扩展性的解决方案。这只是一个对我们有用的快速例子。