带回调的 SuiteScript 2 Http 请求

jk1*_*960 3 netsuite suitescript

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

eri*_*ugh 5

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

来自 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
    });
})
Run Code Online (Sandbox Code Playgroud)


W3B*_*GUY 5

这是我拥有的一个非常基本的(减去有效负载中的许多额外字段),我使用它向 Salesforce 发送 NetSuite 项目,然后使用 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
  }
});
Run Code Online (Sandbox Code Playgroud)

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