Hello, I wanted to see if anyone had any suggestions. I am trying to create a custom inventory where I have an On Hand count, Total QTY of items on Sales Order and stock (onHand - ItemsonSalesOrders). See attached. The reason is that understanding what is actually stock with the committed qty and available is really confusing for users. I got the onhand and the stock scripts down. The only issue is with the script with the items on sales order. The script works when a sales order is created or edited, it takes each item and search all transaction it is on and adds the qty to the custom field. THe script works except when I have 30 or 40 items on a sales order, the script errors exceeding the limits. Is there a better way I can create the script or another way that I am not thinking about.
/**
- @NApiVersion 2.x
- if (context.type !== context.UserEventType.CREATE && context.type !== context.UserEventType.EDIT) return; var fulfillment = context.newRecord; var locationId = fulfillment.getValue({ fieldId: 'location' }); var lineCount = fulfillment.getLineCount({ sublistId: 'item' }); var processedItems = {}; var locationFieldMap = { 10: { onhand: 'custitem21' }, 1: { onhand: 'custitem_nyron_onhand' }, 11: { onhand: 'custitem_nj_onhand' }, 14: { onhand: 'custitem_nh_onhand' }, 15: { onhand: 'custitem_me_onhand' }, 17: { onhand: 'custitem_de_onhand' }, 51: { onhand: 'custitem_md_onhand' }, 44: { onhand: 'custitem_nia_onhand' } }; if (!locationFieldMap[locationId]) { log.debug('Skipped - Location not in map', locationId); return; } var fieldMap = locationFieldMap[locationId]; for (var i = 0; i < lineCount; i++) { var itemId = fulfillment.getSublistValue({ sublistId: 'item', fieldId: 'item', line: i }); if (!itemId || processedItems[itemId]) continue; processedItems[itemId] = true; var itemSearch = search.create({ type: search.Type.INVENTORY_BALANCE, filters: [ ['location', 'anyof', locationId], 'AND', ['item', 'anyof', itemId] ], columns: [ search.createColumn({ name: 'onhand', summary: 'SUM' }) ] }); var results = itemSearch.run().getRange({ start: 0, end: 1 }); if (results.length > 0) { var totalOnHand = parseFloat(results[0].getValue({ name: 'onhand', summary: 'SUM' })) || 0; try { var itemRec = record.load({ type: record.Type.INVENTORY_ITEM, id: itemId }); itemRec.setValue({ fieldId: fieldMap.onhand, value: totalOnHand }); itemRec.save(); log.debug('Updated Item On Hand', 'Item ID: ' + itemId + ' | On Hand: ' + totalOnHand); } catch (e) { log.error('Error Updating Item Record', 'Item ID: ' + itemId + ' | ' + e.message); } } } } catch (e) { log.error('Script Error', e.message); }function afterSubmit(context) {
try {
log.debug('Script Triggered', 'Context Type: ' + context.type);}return {
afterSubmit: afterSubmit
};
});