0

I have the following code which executes a search, then just stops.

for (var i=1; i<=numberItems; i++ ) {
        nlapiInsertLineItem(SUBLIST_Items,1);
        var itemID = vendorItems[i].getId();
        nlapiSetCurrentLineItemValue(SUBLIST_Items,'custrecordvpri_item',itemID );
        var avgCost  = Round(nlapiLookupField(itemType,itemID,'averagecost'),4);
        var stdCost  = Round(nlapiLookupField(itemType,itemID,'custitem_costrepl'),4);
        var lastCost = Round(nlapiLookupField(itemType,itemID,'lastpurchaseprice'),4);
        if (isNaN(avgCost))  { avgCost  = '' };
        if (isNaN(stdCost))  { stdCost  = '' };
        if (isNaN(lastCost)) { lastCost = '' };
        nlapiSetCurrentLineItemValue(SUBLIST_Items,'custrecordvpri_costavg',  avgCost );
        nlapiSetCurrentLineItemValue(SUBLIST_Items,'custrecordvpri_costlast', lastCost );
        nlapiSetCurrentLineItemValue(SUBLIST_Items,'custrecordvpri_coststd',  stdCost );
        nlapiSetCurrentLineItemValue(SUBLIST_Items,'custrecordvpri_vendorcurrency',vendorItems[i].getValue('vendorpricecurrency'));
        nlapiSetCurrentLineItemValue(SUBLIST_Items,'custrecordvpri_currentprice',vendorItems[i].getValue('vendorcost'));
        nlapiCommitLineItem(SUBLIST_Items);
    }   

It is all running as a client script, triggered by a button on the supplier record.

There are some sub functions within this functon (ie. a search etc...).

I cannot find a reason why the code would stop.

if I comment out the "while" loop, it executes the new window etc... and the main record (VprRecord) is created, but with no sublist items.

Is there something I'm missing here?? I'm not an experienced JS programmer, but the basics are there. Is there a limited number of nested functions permitted or something like that?

I only have the one record creation, so governance shouldn't be an issue.

Adding my search function which returns the search result object:

function getVendorItems(vendorid) {
        try {
            var filters = new Array();
            var columns = new Array();
            filters[0] = new nlobjSearchFilter('vendorcost', null, 'greaterthan', 0);
            filters[1] = new nlobjSearchFilter('internalid', 'vendor', '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');
            //columns[5] = new nlobjSearchColumn('preferredvendor');
            var searchresults = nlapiSearchRecord('item', null, filters, columns );
            return searchresults;
        } catch (err) { logError(err,'VPR_getVendorItems: (Vendor: '+vendorid+')' ) }
    } 
Steve Reeder
  • 980
  • 16
  • 42

1 Answers1

1

The code does not execute because there are unhandled javascript errors.

put your code in try catch block and on error log on console eg: try { ...} catch(e){console.dir(e);}

Use browser's console using F12 to see the error

Also, make sure that you operate on searchResults as array not nlobjSearchResult

prasun
  • 7,073
  • 9
  • 41
  • 59
  • now I'm getting.............'TypeError: a.toLowerCase is not a function at nsapiGetRecord (https://system.netsuite.com/javascript/NLAPI.jsp__NS_VER=2015.2.0&minver=98&locale=en_AU.nlqs:1:101274)' This is after the first attempted insertion. – Steve Reeder Dec 30 '15 at 09:46
  • this happens if you are passing wrong arguments, whats the exact statement? – prasun Dec 30 '15 at 09:55
  • I need to know the specific line on which you are getting this error. – prasun Dec 30 '15 at 10:00
  • it appears `nlapiLookupField(itemType,itemID,'averagecost')` either of the 1st two arguments are `undefined` – prasun Dec 30 '15 at 10:09
  • 1
    Beautiful... Running now... Legendary assistance!! My thanks to you.... I've learnt more from you @prasun in that last few days, than I think I would from an Aussie NS training program. I know it's off topic, is there an easy way to display progress of a function like this? – Steve Reeder Dec 30 '15 at 10:14
  • easiest way is `console.log()` and see in console. otherwise use fancy HTML/CSS/JS library to display a progress bar – prasun Dec 30 '15 at 10:16
  • inserting line items should not ideally take time considerable time, but the final `nlapiSubmitRecord()` iteself may take considerable time. Is it taking too long? – prasun Dec 30 '15 at 10:22
  • actually you are doing `nlapiLookupField` which has a governance cost of about 4 points, so in your case 12 points per sublist – prasun Dec 30 '15 at 10:25
  • write a search and use the searchResult repetitively than firing a `search/lookup` in each field – prasun Dec 30 '15 at 10:43
  • what's odd now, is the sublist lines are not attaching to the main record. I'll create a new question and link to this one. – Steve Reeder Dec 30 '15 at 11:04
  • yes, please do that, it is creating too much of comments – prasun Dec 30 '15 at 11:10
  • http://stackoverflow.com/questions/34528400/submitted-sublist-lines-on-custom-record-custom-subrecord-not-linking-to-main-re – Steve Reeder Dec 30 '15 at 11:14