Suitescript Code stops for no apparent Reason

2019-08-22 02:54发布

问题:

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

回答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