Nour Khaled
04/29/2025, 2:04 PMCeligo AI
04/29/2025, 2:05 PMAI generated answer (using internal Slack threads):The function you provided is already in SuiteScript 2.0. However, if you want to refactor it, you can consider the following changes: 1. Use
search.createFilter
and search.createColumn
directly instead of using search.create
and then defining filters and columns inside it. This can make your code cleaner and easier to read.
2. Use Array.prototype.map
and Array.prototype.filter
to process the search results instead of using search.run().each
. This can make your code more functional and easier to understand.
Here is a refactored version of your function:
javascript
function getItemLots(itemId, locationId, year) {
var itemLotArr = [];
var itemExpDateArr = [];
var filters = [
search.createFilter({name: 'onhand', operator: search.Operator.GREATERTHAN, values: '0'}),
search.createFilter({name: 'formulatext', operator: <http://search.Operator.IS|search.Operator.IS>, values: year, formula: 'SUBSTR({inventorynumber.inventorynumber}, LENGTH({inventorynumber.inventorynumber}) - 1, 2)'}),
search.createFilter({name: 'item', operator: search.Operator.ANYOF, values: itemId}),
search.createFilter({name: 'location', operator: search.Operator.ANYOF, values: locationId})
];
var columns = [
search.createColumn({name: 'internalid', join: 'item'}),
search.createColumn({name: 'inventorynumber', join: 'inventorynumber'}),
search.createColumn({name: 'expirationdate', join: 'inventorynumber'})
];
var lotSearch = search.create({type: search.Type.INVENTORY_BALANCE, filters: filters, columns: columns});
lotSearch.run().each(function(result) {
var currLot = result.getValue({name: 'inventorynumber', join: 'inventorynumber'});
var currExpDate = result.getText({name: 'expirationdate', join: 'inventorynumber'});
if(!isEmpty(currLot)) {
itemLotArr.push(currLot);
}
if(currExpDate !== null) {
itemExpDateArr.push(currExpDate);
}
return true;
});
return {
lots: itemLotArr,
expDate: itemExpDateArr
}
}
******
Disclaimer:
This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes.
******Celigo AI
04/29/2025, 2:05 PMSlack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.854966402
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.84999913
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.849731863
Celigo AI
04/29/2025, 2:05 PM