Friday, September 21, 2018

Automatically append a FixedLine Item whenever a New Item is added in the Sublist via Script

In the Sales Order, customer wants to add a fixed line item whenever a new Item is added. The fixed line item should always be above the new Item  

  1. Create a Client Script
  2. Deploy on the recalc function of the Sales Order record.

The script will always add a fixed line item above the new Item added. 
 
The problem that would be encountered here is that this causes infinite loop. The workaround is to use a flag that would determine if the new item added came from the UI (user clicked on the Add button) or added by the script.  

var flag = 'manual'; //added by User
function onRecalc(type)
{
  if (type!='item') return;
    if (flag!='script')
    {
     flag='script'; //added by script
     var line = nlapiGetCurrentLineItemIndex('item');
     nlapiInsertLineItem('item',line);
     nlapiSetCurrentLineItemValue('item','item', 11, false, true); // 11 is the internal id of the fixed line item
     nlapiCommitLineItem('item');
     flag='manual'; //added by User
    }
}
 


 

No comments:

Post a Comment