0

重新输入代码如下,结合来自@bknights 和@prasun 的建议

function main_GetVendorItems(request, response) {
    response.write(JSON.stringify(getVendorItems(request)));
}

function getVendorItems(request) {

    var vendorid = request.getParameter('vendor');

    nlapiLogExecution('DEBUG', 'searchRes', 'Searching For Vendor ID: '+vendorid );

    var filters = new Array();
    var columns = new Array();
    filters[0] = new nlobjSearchFilter('vendorcost', null, 'greaterthan', 0);
    filters[1] = new nlobjSearchFilter('othervendor', null, 'is', [vendorid] );
    columns[0] = new nlobjSearchColumn('itemid');
    columns[1] = new nlobjSearchColumn('entityid', 'vendor');
    columns[2] = new nlobjSearchColumn('vendorcost');
    columns[3] = new nlobjSearchColumn('vendorcode');
    columns[4] = new nlobjSearchColumn('vendorpricecurrency');

    var searchresults = nlapiSearchRecord('item', null, filters, columns );

    //for test test each element of the array
    (searchresults || []).forEach(function(res){
        nlapiLogExecution('DEBUG', 'searchRes', res instanceof nlobjSearchResult);
    })

    return searchresults;
}

调用函数如下:

function test () {
    var vendorID = nlapiGetFieldValue('custrecordvpr_supplier'); alert('searching for vendor ID: '+vendorID );
    var url = nlapiResolveURL('SUITELET', 'customscriptls_getvendoritems', 'customdeployls_getvendoritems', true);
    var params = {}
    params['vendor'] = vendorID;
    var response = nlapiRequestURL(url, params);
    var VendorItemsSublist = response.getBody();

    nlapiSetFieldValue('custrecordvpr_comment',VendorItemsSublist );        
}

我的自定义记录/表单上有一个注释字段,显示返回的对象。在上面的代码中,真正奇怪的是,我没有将任何信息添加到执行日志中,即使是它应该发布正在搜索的供应商 ID 的第一个条目。

我检查了脚本和部署记录,没有任何不妥或不妥之处……我必须遗漏一些非常简单的东西。

顺便说一句,我的自定义表单上的“测试”按钮正在调用代码。

4

3 回答 3

0

看起来您没有正确写入 Suitelet 中的响应对象。

function main_GetVendorItems(request, response) {
    response.write(JSON.stringify(getVendorItems(request)));
}

function getVendorItems(request) {
var vendorid = request.getParameter('vendorid');

var filters = new Array();
var columns = new Array();
filters[0] = new nlobjSearchFilter('vendorcost', null, 'greaterthan', 0);
//filter should be on vendor
filters[1] = new nlobjSearchFilter('vendor', null, 'anyof', vendorid );
columns[0] = new nlobjSearchColumn('itemid');
columns[1] = new nlobjSearchColumn('entityid', 'vendor');
columns[2] = new nlobjSearchColumn('vendorcost');
columns[3] = new nlobjSearchColumn('vendorcode');
columns[4] = new nlobjSearchColumn('vendorpricecurrency');

var searchresults = nlapiSearchRecord('item', null, filters, columns );

//for test test each element of the array
(searchresults || []).forEach(function(res){
  nlapiLogExecution('DEBUG', 'searchRes', res instanceof nlobjSearchResult);
})

return searchresults;
}

另外,请确保在请求参数中指定了供应商 ID

var url = nlapiResolveURL('SUITELET', 'customscriptls_getitemvendors', 'customdeploy_getitemvendors', true);
var params = {}
params['itemid'] = itemID ;    // itemID is passed to this function.
params['vendorid'] = vendorID ;    // vendorID is passed to this function.
var response = nlapiRequestURL(url, params);
var itemVendorSublist = response.getBody();
于 2015-12-28T10:02:40.160 回答
0

如果您尝试查询,item vendor那么只需尝试

filters[1] = new nlobjSearchFilter('vendor', null,'anyof', vendorid );
于 2015-12-28T10:25:43.633 回答
0

这适用于控制台窗口。我怀疑您没有在参数中提供数字内部 id:

var vendorid = 43; // or 32 values from my test account. Want to confirm that the code works whether vendor is item's preferred vendor or not.
nlapiSearchRecord('item', null, [
    new nlobjSearchFilter('vendorcost', null, 'greaterthan', 0),
    new nlobjSearchFilter('internalid', 'vendor', 'anyof', [vendorid]), //numeric value
    new nlobjSearchFilter('internalid', null, 'is', '42') // limit results for testing
], [
    new nlobjSearchColumn('itemid'),
    new nlobjSearchColumn('entityid', 'vendor'),
    new nlobjSearchColumn('vendorcost'),
    new nlobjSearchColumn('vendorcode'),
    new nlobjSearchColumn('vendorpricecurrency')
]).forEach(function(it) {
    console.log("%s from %s", it.getValue('itemid'), it.getValue('entityid', 'vendor'));
});

尽管自 2002 年以来我一直在使用 Netsuite,但我从未直接从 Suitelet 返回一组搜索结果。我最近在这个论坛上的人们的回答中看到了几次,但我仍然觉得它有点有趣。

如果我写这篇文章,我会倾向于做以下事情:

var results = (nlapiSearchRecord(...) || []).map(function(res){
  return { id:res.getId(), vendorName: res.getValue('entityid', 'vendor')...};
});

response.setContentType('JAVASCRIPT');
response.write(JSON.stringify(results));

其实一般多一点。我有一个崇高的文本片段,用于处理几个常见 JSONP 模式的套件响应(例如,如果您从网站调用它):

function _sendJSResponse(request, response, respObject){
    response.setContentType('JAVASCRIPT');
    var callbackFcn = request.getParameter("jsoncallback")  || request.getParameter('callback');
    if(callbackFcn){
        response.writeLine( callbackFcn + "(" + JSON.stringify(respObject) + ");");
    }else response.writeLine( JSON.stringify(respObject) );
}
于 2015-12-28T18:21:56.867 回答