theinvisibleduck
theinvisibleduck

Reputation: 187

How do I make sure the value is set on the line that I am on in Netsuite Onfieldchange

In netsuite I am trying to automatically calculate the margin on the item sublist on a sales order and set a price level based off of the nearest margin and then show the cost and price level to the user. I have everything working except if the user enters in a margin, and then clicks off onto another row, then it adds the calculated average cost and price level to the row that you clicked on rather than the row the information was added to. How do I guarantee that it adds the value to the line I enter the margin in on?

if (name == 'custcol61') {
    var margincalcfield = nlapiGetCurrentLineItemValue('item', 'custcol61');

    if (margincalcfield > 0 && margincalcfield * 1 == margincalcfield) {
        var uid = nlapiGetCurrentLineItemValue('item', 'item');
        var lineitemid = nlapiGetCurrentLineItemValue('item', 'line');

        var itemRecord = nlapiLoadRecord('inventoryitem', uid);
        var preferedvendorrate = itemRecord.getFieldValue('cost');
        var itemaveragecost = itemRecord.getFieldValue('averagecost');
        var newPG = nlapiGetCurrentLineItemValue('item', 'custcol61') / 100;
        var itemBP = itemRecord.getLineItemMatrixValue('price', 'price', 1, 1);
        var itemPG = itemRecord.getFieldValue('pricinggroup');
        var newSP = (preferedvendorrate / (1 - newPG)).toFixed(2);
        var newPriceLevel = 1 - ((newSP / itemBP).toFixed(2));

        // alert(itemBP );
        // alert(newSP );
        // alert(newPriceLevel);

        var pricelineCount = parseInt(itemRecord.getLineItemCount('price'));
        for (x = 1; x <= pricelineCount; x++) {
            var pricelevelprice = itemRecord.getLineItemMatrixValue('price', 'price', x, 1);
            var priceleveldiscount = itemRecord.getLineItemValue('price', 'discount', x);
            var pricelevel = itemRecord.getLineItemValue('price', 'pricelevel', x);

            if (newSP >= pricelevelprice && priceleveldiscount != null) {
                if (newSP != pricelevelprice) {
                    var y = x - 1;
                    var pricelevel = itemRecord.getLineItemValue('price', 'pricelevel', y);
                    var pricelevelprice = itemRecord.getLineItemMatrixValue('price', 'price', y, 1);
                }

                // alert(pricelevelprice );
                // nlapiSelectLineItem(type, lineitemid );
                nlapiSetCurrentLineItemValue('item', 'price', pricelevel);

                // /////-----------------------------start set margin values-------------------------------------

                var ppMargin = ((1 - (preferedvendorrate / pricelevelprice)) * 100).toFixed(2);
                var avgcostMargin = ((1 - (itemaveragecost / pricelevelprice)) * 100).toFixed(2);

                if (itemaveragecost > 0) {
                    var avgmargintext = "\nAC:" + avgcostMargin + "%";
                }

                var purchasepricemargintext = "PP:" + ppMargin + "% ";
                var marginfieldvalue = purchasepricemargintext + avgmargintext;
                nlapiSetCurrentLineItemValue('item', 'custcol61', marginfieldvalue);

                // /////-----------------------------end set margin values-------------------------------------

                return true;
            }
        }
    }
}

I suspect that it has to do with getting the currentline item and setting the value on that instead of the "nlapiSetCurrentLineItemValue" but so far I havent been able to get it to work.

Upvotes: 0

Views: 1055

Answers (1)

erictgrubaugh
erictgrubaugh

Reputation: 8847

Your suspicion is correct; apparently the fieldChanged event does not finish before the selection of the new line. Your fieldChanged event handler will get passed three parameters, in this order:

  1. type - The ID of the sublist that changed
  2. name - The ID of the field that changed
  3. linenum - The index of the line that changed

The last parameter is what you're interested in. Make sure your fieldChanged handler function accepts three parameters, and use the linenum parameter in combination with nlapiSetLineItemValue instead of setCurrentLineItemValue.

Upvotes: 1

Related Questions