Hello Guru's,
I have been trying to populate the results of a transaction search to a custom item field via scheduled suitescript. I am able to get the script to execute, but I am struggling with the correct GetValue. Because it is a transaction search, and the item is on the transaction line, I can not seem to get the right item id or item type. (the search is for all inventory items including serial and lot numbered). I am not sure what I am doing wrong, Will someone please take a look?
/**
@NApiVersion 2.x@NScriptType ScheduledScript
*/
define(['N/search', 'N/record', 'N/log'], function(search, record, log) {
function execute(context) {
// Load your existing saved search
var searchResults = search.load({ id: 'customsearch_avg_daily_sales' }); // Replace with your saved search ID
// Run the search and process each result
searchResults.run().each(function(result) {
// Retrieve the internal ID and type of the item from transaction line level
var itemInternalId = result.getValue({ name: 'item', join: 'transactionLine', summary: 'GROUP' }); // Adjusted join to 'transactionLine'
var itemType = result.getValue({ name: 'type', join: 'transactionLine', summary: 'GROUP' }); // Adjusted join to 'transactionLine'
var avgDailySales = result.getValue({ name: 'formulanumeric', summary: 'SUM' }); // Ensure this matches your saved search formula field
// Log retrieved values to debug
log.debug('Search Result', 'Item Internal ID: ' + itemInternalId + ', Item Type: ' + itemType + ', Avg Daily Sales: ' + avgDailySales);
// Check if itemInternalId and itemType are correctly retrieved
if (itemInternalId && itemType) {
// Map the item type to the appropriate record type in SuiteScript
var recordType;
switch (itemType) {
case 'InvtPart': // Inventory Item
recordType = record.Type.INVENTORY_ITEM;
break;
case 'SerializedInvtPart': // Serialized Inventory Item
recordType = record.Type.SERIALIZED_INVENTORY_ITEM;
break;
case 'LotNumberedInvtPart': // Lot Numbered Inventory Item
recordType = record.Type.LOT_NUMBERED_INVENTORY_ITEM;
break;
case 'Assembly': // Assembly Item
recordType = record.Type.ASSEMBLY_ITEM;
break;
default:
log.error('Unknown Item Type', 'Item type ' + itemType + ' is not handled by the script.');
return true; // Skip this record and continue
}
// Update the item record with the calculated average daily sales
record.submitFields({
type: recordType,
id: itemInternalId,
values: {
custitem_avg_daily_sales_volume: avgDailySales // Use your custom field ID
}
});
} else {
log.error('Item Internal ID or Type Missing', 'Unable to find item internal ID or type for search result');
}
return true; // Continue iteration
});
}
return {
execute: execute
};
});