Hello,
Currently we have 2 inventory locations (let's call them "Stock" and "LPO") which I am trying to do the following with: On a purchase order, each line item needs to show the current on hand quantity available in each location.
There is a standard "On hand" column available in netsuite, but this either shows the total on hand available in all locations if the "location" column is blank on an item; or if we select a specific location on an item in the PO then this column will show only the quantity available in that location. While we need to see the quantity available in 2 different locations as stated above, so this standard column does not solve our requirement.
So far, i've done the following:
1. Created custom transaction column fields called "On hand stock" and "On hand LPO"
2. Created a script that looks at each item, runs a search on the item in each of the locations mentioned above, and stores the "location on hand" quantities in the custom fields that we've set up in #1
The script is executed in 2 places:
1. client side script on purchase order: postSourcing (type=item) whereby whenever an item is entered on each line, this script runs and calculates the quantities and they are instantly available to see (this works fine)
2. user event script on purchase order: beforeLoad (type=view), so that every time a purchase order is viewed, the script loops through all the items and updates these 2 custom fields so that these values remain up to date in case other purchase orders are received
The problem I am having is with the beforeLoad function, the script seems to be working fine but the updated values are not shown on the first time the purchase order is viewed, I have to refresh the PO to see the updated values. Can anyone help in this ? My script is as follows:
function blPurchaseOrder(type, form) { if(type != 'view') { return;} var subsidiary = nlapiGetFieldValue('subsidiary'); var subs = subKSA.toString(); //subKSA is a global var containing KSA subsidiary id // run this only for KSA subsidiary if(subs.indexOf(subsidiary) > -1) { var recordType = nlapiGetRecordType(); recordId = nlapiGetRecordId(); var record = nlapiLoadRecord(recordType, recordId); var count = record.getLineItemCount('item'); for(var i=1; i<=count;i++) { debug( D, "Loop #", i); var item = nlapiGetLineItemValue('item','item',i); var filters = []; // ONHAND STOCK START- calculate on hand quantity for DAZ RUH STOCK location // set filters for search filters[0] = new nlobjSearchFilter('inventorylocation', null, 'anyof', [37]); // RUH STOCK location id is 37 filters[1] = new nlobjSearchFilter('internalid',null,'anyof',item); // search item record using above filters and look for location quantity on hand var stockResults = nlapiSearchRecord('item',null,filters, new nlobjSearchColumn('locationquantityonhand', null, 'sum')); if(stockResults) { var onhandstock = stockResults[0].getValue('locationquantityonhand', null, 'sum'); // if search result returns available quantity on hand, set custom field to the result, else set custom field to 0 if(onhandstock) { record.setLineItemValue('item','custcol_onhand_ruhstock', i, onhandstock); } else { record.setLineItemValue('item','custcol_onhand_ruhstock', i, 0); } } // ONHAND STOCK END // ONHAND LPO START- calculate on hand quantity for DAZ RUH LPO location // set filters for search filters[0] = new nlobjSearchFilter('inventorylocation', null, 'anyof', [12]); // RUH LPO location id is 12 filters[1] = new nlobjSearchFilter('internalid',null,'anyof',item); // search item record using above filters and look for location quantity on hand var stockResults = nlapiSearchRecord('item',null,filters, new nlobjSearchColumn('locationquantityonhand', null, 'sum')); if(stockResults) { var onhandlpo = stockResults[0].getValue('locationquantityonhand', null, 'sum'); // if search result returns available quantity on hand, set custom field to the result, else set custom field to 0 if(onhandlpo) { record.setLineItemValue('item','custcol_onhand_ruhlpo', i, onhandlpo); } else { record.setLineItemValue('item','custcol_onhand_ruhlpo', i, 0); } } // ONHAND LPO END } // loop end nlapiSubmitRecord(record, true, true); } }