0
votes

I have a client script which is doing two things:

  1. Calculate total weight of sales order on add of line
  2. Copy tax code from custom field to native field

The script deploys correctly when adding lines in the UI from the sublist but when using the "add multiple" button and selecting and adding multiple lines at once, the script does not trigger. Here is the script as I have it written so far (I have 2 versions, one which is validateLine and one which is postSourcing).

Validate Line:

function calculateTotalWeight(type){

      var lines = nlapiGetLineItemCount('item');
      var totalWeight = 0 ;

      for(var i=1; i< lines+1 ; i++){   
           var weight = nlapiGetLineItemValue('item', 'custcol_itemweight', i);
           var quantity = nlapiGetLineItemValue('item', 'quantity', i);
           var weightTimesQuantity = weight * quantity;

           totalWeight = totalWeight + weightTimesQuantity ;

      }
      nlapiSetFieldValue('custbody_items_total_weight', totalWeight);

}
function validateLine(type){

        var taxableCustomer = nlapiGetFieldValue('custbody_taxable');

        if (taxableCustomer == 'T'){
            var customTax = nlapiGetCurrentLineItemValue(type,'custcol_taxcode');
            nlapiLogExecution('DEBUG', 'Custom Tax Value',customTax);
            nlapiSetCurrentLineItemValue('item','taxcode',customTax,true,true);
        }
        return true;
}

postSourcing:

function calculateTotalWeight(type){

      var lines = nlapiGetLineItemCount('item');
      var totalWeight = 0 ;

      for(var i=1; i< lines+1 ; i++){   
           var weight = nlapiGetLineItemValue('item', 'custcol_itemweight', i);
           var quantity = nlapiGetLineItemValue('item', 'quantity', i);
           var weightTimesQuantity = weight * quantity;

           totalWeight = totalWeight + weightTimesQuantity ;

      }
      nlapiSetFieldValue('custbody_items_total_weight', totalWeight);

}
function postSourcing(type, name)
{

  if(type === 'item' && name === 'item')
  {
    var custcol_taxcode = nlapiGetCurrentLineItemValue('item', 'custcol_taxcode');
    var line = nlapiGetCurrentLineItemIndex(type);

    {
        nlapiSetCurrentLineItemValue('item', 'taxcode', custcol_taxcode);
    }
  }
}

How can I get this script to trigger with the add multiple button?

1

1 Answers

0
votes

You’ll need to calculate the weight on the recalc event. The following is from a script that works as a scriptable cart/checkout script. It can be deployed in an eCommerce context or the UI context. (i.e. a deployed client script as opposed to a client script attached to a form)

Note:You should set up your tax codes so that they are assigned automatically. It is possible to script those but it's a fair pain to do.

the field custbody_sco_toggle is a checkbox field that keeps the script out of an infinite loop if your recalc scripts might change the order total.

var scriptableCart = (function(){

var cartScript = {};

var isUI = ('userinterface' == nlapiGetContext().getExecutionContext());
var isWeb = !isUI;

function tty(type, title, detail){
    var haveWindow = typeof window != 'undefined';
    if(isUI && haveWindow && window.console) window.console.log(title, detail);
    else if(isWeb || !haveWindow) nlapiLogExecution(type, title, (detail || '') +' '+entranceId +' '+nlapiGetContext().getExecutionContext()); // this slows down the NS GUI
}

function calculateTotalWeight(type){...}

cartScript.recalc = function(type){
    tty("DEBUG", "entered "+ type +" with toggle: "+ nlapiGetFieldValue('custbody_sco_toggle'));
    if('F' == nlapiGetFieldValue('custbody_sco_toggle')){
        try{
            nlapiSetFieldValue('custbody_sco_toggle', 'T', false, true);
            if(type == 'item'){
                calculateTotalWeight(type);
            }
        }catch(e){
            tty('ERROR', 'In recalc for '+ type, (e.message || e.toString()) + (e.getStackTrace ? (' \n \n' + e.getStackTrace().join(' \n')) : ''));
        }finally{
            nlapiSetFieldValue('custbody_sco_toggle', 'F');
        }
    }
};

return cartScript;
})();