2016-08-31 61 views
1

嗨我需要与外部设备进行交互以通过http传输数据。我知道SuiteScript 1有限制,但SuiteScript 2又如何?有没有办法使用有效载荷进行HTTP请求并在2.0中回拨感谢您提前给予的帮助SuiteScript 2 Http请求回拨

回答

2

这是一个非常基本的,我有(减去在有效载荷很多额外的字段),我用它来发送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所言,承诺是一个更具可扩展性的解决方案。这只是一个适合我们的快速例子。

+1

非常感谢我所需要的 – jk121960

+0

我很抱歉我应该马上问这个问题,但这是服务器还是客户端?因为我必须定期从脚本运行这个,谢谢 – jk121960

+0

这个代码是服务器端。我有一个用户事件脚本运行。计划的脚本可以使用相同的基本功能。您只需循环调用,或者构建一组数据并发送。然后循环查看结果。 – W3BGUY

2

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

从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 
    }); 
})