Suitescript将供应商提供的所有商品作为搜索对象返回

时间:2015-12-28 09:51:13

标签: netsuite suitescript

重新输入以下代码,并结合@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的第一个条目。

我已经检查了脚本和部署记录,没有什么不妥或有什么不妥...我必须要丢失一些非常简单的东西。

顺便提一下,代码是由"测试"我的自定义表单上的按钮。

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

如果您尝试查询item vendor,请尝试

filters[1] = new nlobjSearchFilter('vendor', null,'anyof', vendorid );

答案 2 :(得分: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));

实际上通常会多一点。我有一个sublime文本片段,我用于处理一些常见的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) );
}