2016-08-31 63 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 
    }); 
})