我有一个预定的脚本,它调用 API 并获得放入销售订单的响应。如果要搜索的记录少于 10 条,则工作正常,但我收到以下错误:
{"type":"error.SuiteScriptError","name":"SSS_REQUEST_TIME_EXCEEDED","message":"您尝试连接的主机已超过最大允许响应时间。",
API 文档说:
请给我们时间来处理您的请求。如果你也发送 许多请求太快,我们将以 429 错误代码响应 (TooManyRequests),您的应用程序不应再发送 请求并等待 120 秒。
每个帐户的每个 API 每秒只能有 3 或 10 个请求。 实际上,这意味着您必须(如果可能)对用户进行身份验证,以便 该限制远远超出了给定用户的范围。
下面是我的代码。我怎样才能解决这个问题,让它在继续下一批之前等待 120 秒?
define(['N/search', 'N/record', 'N/https'], function(search, record, https) {
function execute(context) {
// Load the saved search
var savedSearchId = 2808;
var savedSearch = search.load({
id: savedSearchId
});
// Run the search and process the results
savedSearch.run().each(function(result) {
// Get the Sales Order ID from the search result
var salesOrderId = result.getValue({
name: 'internalid'
});
var mawb = result.getValue({
name: 'custbody37'
});
// Load the Sales Order record
var salesOrder = record.load({
type: record.Type.SALES_ORDER,
id: salesOrderId
});
// Call the API with the field value from the Sales Order record and token in the header
var fieldValue = salesOrder.getValue('custbody37');
var url = 'https://api.XXXXXX.com/v4/XXXX';
var headers = {
'Tracking-Api-Key': 'XXXXXXX',
'Content-Type': 'application/json'
};
var response = https.post({
url: url,
headers: headers,
body: JSON.stringify({
"awb_number": fieldValue
})
});
log.debug({
title: 'API Response',
details: response.body
});
// Parse the response to get the value of the last_event field
var apiResponse;
try {
apiResponse = JSON.parse(response.body);
} catch (e) {
log.error({
title: 'Error parsing API response',
details: e
});
return true; // continue processing search results
}
if (!apiResponse || !apiResponse.data || !apiResponse.data.last_event) {
log.error({
title: 'API response is missing last_event field',
details: apiResponse
});
return true; // continue processing search results
}
var lastEventValue = apiResponse.data.last_event;
// Update a different field in the Sales Order with the last_event value
salesOrder.setValue({
fieldId: 'custbody417',
value: lastEventValue
});
// Save the Sales Order record
try {
salesOrder.save();
} catch (e) {
log.error({
title: 'Error saving Sales Order record',
details: e
});
return true; // continue processing search results
}
// Continue processing search results
return true;
});
}
return {
execute: execute
};
});