Hi everyone - I am using the following code to reallocate orders to the location where the correct stock is available, and if no location has available falling back to location '8'.
However - There are times when we need to override the system and choose a specific location - with the below script in place - it undoes out changes. How can I set this to ONLY run on Creation of a new order, and not on editing an existing one?
// BEGIN CUSTOM FUNCTIONS FOR SETTING LOCATIONS FOR WEB ORDERS // This custom function is run immediately before a sales order is saved. function customBeforeSubmit() { debug('Before Submit is called'); return chooseLocation(); } //END CUSTOM FUNCTIONS FOR SETTING LOCATION //BEGIN LOCATION function chooseLocation() { // Get entire quantities of all inventoried items in the sales order. Note that optional filtering // criteria is used when calling the getItemMap() function to exclude noninventory items. var itemMap = getItemMap('NonInvtPart'); var numLines = nlapiGetLineItemCount('item'); var goodLocations = new Array(); var badLocations = new Array(); // Get available quantities for each location of each item on the order. for (itemId in itemMap) { debug('Loading record for item id '+itemId); var rec = nlapiLoadRecord('inventoryitem', itemId); debug('Item record '+itemId+' loaded - getting location count'); var locCount = rec.getLineItemCount('locations'); debug('Starting loop for '+locCount+' locations'); for (var i=1; i <= locCount; i++) { var locId = rec.getLineItemValue('locations', 'location', i); var onhand = rec.getLineItemValue('locations', 'quantityavailable', i); // Current quantity of item is stored under itemMap[itemId] if there is enough quantity // available for this order at this location, add it to the good location list. if (itemMap[itemId] <= onhand) { debug('Location '+locId+' has '+onhand+' available - added to good list'); goodLocations[locId] = true; } // If there is not enough quantity for the item, then add this location to the bad location list. if (itemMap[itemId] > onhand) { debug('Location '+locId+' has '+onhand+' available - added to bad list'); badLocations[locId] = true; } } } // Take any bad locations out of the good list. for (locId in badLocations) { delete goodLocations[locId]; } var finalLocation = 8; // default in the case of no good locations found for (goodLoc in goodLocations) { debug('Location '+goodLoc+' can provide for all items in the sales order - setting location to this'); finalLocation = goodLoc; break; } nlapiSetFieldValue('location', finalLocation, true, true); return true; } // END LOCATION // BEGIN UTILITY FUNCTIONS function isEmpty(val) { return (val == null || val == ''); } function isNotEmpty(val) { return !isEmpty(val); } // This function will return a map with the internal IDs for the item as the key and the total // quantities for that item in the value. It will sum up any items in the items sublist on the sales // order. If passed a non-null filter value, will exclude item types that match the filter. function getItemMap(excludeFilter) { if (isNotEmpty(excludeFilter)) { debug('getItemMap called with filter '+excludeFilter); } var itemMap = new Array(); var numLines = nlapiGetLineItemCount('item'); for (var i=1; i<= numLines; i++) { // Include only items that match the filter. var itemType = nlapiGetLineItemValue('item','itemtype',i); if (isNotEmpty(excludeFilter) && itemType == excludeFilter) { debug('Ignoring line '+i+', itemType '+itemType+' excluded'); continue; } var itemId = nlapiGetLineItemValue('item','item',i); var nextQty = parseInt(nlapiGetLineItemValue('item','quantity',i)); debug('Item '+itemId+' of type '+itemType+' added to item map'); if (itemId in itemMap) { // Add the quantities together if other line items in the items sublist on the sales order exist // with the same item. nextQty = nextQty + parseInt(itemMap[itemId]); } debug('Item Id '+itemId+' has updated quantity to '+nextQty+' because of line #'+i); itemMap[itemId] = nextQty; } return itemMap; } function debug(val) { nlapiLogExecution('AUDIT', val); } // END UTILITY FUNCTIONS