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-------------------------------------
retu 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.
