0

Re-entering the code as follows, with combined suggestions from @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;
}

The calling function as below:

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

I've got a comment field on my custom record/form which shows the returned object. On the code above, what's really strange, is I'm not getting any information being added to the execution log,even the first entry where it should post the vendor id being searched for.

I've checked the script and deployment records, and there is nothing untoward or amiss there... I have got to be missing something extremely simple.

Incidentally, the code is being called by a "Test" button on my custom form.

Steve Reeder
  • 980
  • 16
  • 42
  • are you getting `null` in return? – prasun Dec 28 '15 at 09:56
  • I have a test occurring after the search is returned to check if the returned variable is instanceof nlobjSearch which is coming back as false. – Steve Reeder Dec 28 '15 at 10:29
  • you will get `null` or an `Array` iterate over array to check that – prasun Dec 28 '15 at 10:38
  • is that the case when using: if(searchresult instanceof nlobjSearch) {....} – Steve Reeder Dec 28 '15 at 11:40
  • see my answer code on how I am doing that – prasun Dec 28 '15 at 11:47
  • hmm.... I'm getting................... An nlobjSearchFilter contains an invalid operator, or is not in proper syntax: vendor. – Steve Reeder Dec 28 '15 at 12:16
  • this is because you are not passing vendorId in the request parameter – prasun Dec 28 '15 at 12:39
  • This is what i get for working late at night when I'm on holidays. I pasted the wrong function... duh! I've amended the code in my initial question. I am passing vendorid, which I've checked is populated with the correct vendor internal id. – Steve Reeder Dec 28 '15 at 14:45
  • it should work. alternatively, you can also try `filters[1] = new nlobjSearchFilter('othervendor', null, 'anyof', vendorid );` – prasun Dec 28 '15 at 15:04
  • Amended original code – Steve Reeder Dec 29 '15 at 00:08
  • Strangely, I'm getting a resulting value of the search var: ........Notice......You are not allowed to navigate directly to this page. – Steve Reeder Dec 29 '15 at 00:32
  • I've tested the search parameters in a stand-alone search (ie. not a suitelet), and the search is functioning correctly and returning the correct object. Therefore, it must be something to do with my suitelet call. – Steve Reeder Dec 29 '15 at 01:22
  • I would also suggest to make a call to suitelet in a spearate window with URL param `vendorid` and then verify the response that you are getting – prasun Dec 29 '15 at 06:17

3 Answers3

0

It looks like you are not writing properly to the response object in 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;
}

Also, make sure vendor id is specified in request parameter

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();
prasun
  • 7,073
  • 9
  • 41
  • 59
0

If you're trying to query on item vendor then simply try

filters[1] = new nlobjSearchFilter('vendor', null,'anyof', vendorid );
Rockstar
  • 2,228
  • 3
  • 20
  • 39
0

This works in a console window. I suspect you are not supplying the numeric internal id in your parameter:

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

Although I've been working with Netsuite since 2002 I've never returned a set of search results directly from a Suitelet. I've seen that a couple of times recently in people's answers on this forum but I still find it a bit amusing.

If I were writing this I'd have tended to do the following:

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

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

Actually there's generally a little more. I have a sublime text snippet that I use for suitelet responses that handles a couple of common JSONP patterns (e.g. if you were calling this from a website):

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) );
}
bknights
  • 14,408
  • 2
  • 18
  • 31