how to set inventory details on item fulfillment record netsuite suitescript?
I’m working on a map-reduce script to transform sales orders into item fulfillments. While I’ve managed to perform the transformation, I’m unsure about setting inventory details during this process. Specifically, I’m uncertain about the values to be set in the inventory details subrecord. Could someone provide guidance on how to properly set these values?
/**
* @NApiVersion 2.x
* @NScriptType MapReduceScript
*/
define([‘N/search’, ‘N/record’, ‘N/email’], function(search, record, email) {
function getInputData() {
// Define search criteria to find sales orders to be processed
var salesOrderSearch = search.create({
type: search.Type.SALES_ORDER,
filters:
[
[“type”,”anyof”,”SalesOrd”],
“AND”,
[“status”,”anyof”,”SalesOrd:B”],
“AND”,
[“mainline”,”is”,”T”],
“AND”,
[“trandate”,”within”,”10/26/2023″,”2/23/2024″]
],
columns:
[
search.createColumn({name: “internalid”, label: “Internal ID”}),
search.createColumn({name: “tranid”, label: “Document Number”}),
search.createColumn({
name: “email”,
join: “customer”,
label: “Email”
})
]
});
// Return search results
return salesOrderSearch;
}
function map(context) {
// Process each sales order found in the search results
var salesOrder = JSON.parse(context.value);
log.debug(“salesOrder”,salesOrder.id);
// Create invoice record based on sales order
var ifId = createItemFulfillmentFromSalesOrder(salesOrder);
log.debug(“ifId”,ifId);
// Emit the invoice ID for further processing
context.write({
key: salesOrder.id,
value: ifId
});
}
function createItemFulfillmentFromSalesOrder(salesOrder) {
try {
log.debug(“inside createItemFulfillmentFromSalesOrder”);
// Transform sales order to item fulfillment
var itemFulfillmentRecord = record.transform({
fromType: record.Type.SALES_ORDER,
fromId: salesOrder.id,
toType: record.Type.ITEM_FULFILLMENT,
//isDynamic: true // Set to true if you want to interact with the item fulfillment record as a dynamic record
});
log.debug(“itemFulfillmentRecord”, itemFulfillmentRecord);
var IFLines=itemFulfillmentRecord.getLineCount({
sublistId: ‘item’
});
log.debug(“IFLines”,IFLines);
for(var f=0;f<IFLines;f++){
itemFulfillmentRecord.setSublistValue({
sublistId: ‘item’,
fieldId: ‘itemreceive’,
value: true,
line:f
});
var inventoryDetail = itemFulfillmentRecord.getSublistSubrecord({
sublistId: ‘item’,
fieldId: ‘inventorydetail’,
line: 0
});
log.debug(“inventory details”,inventoryDetail);
inventoryDetail.setSublistValue({
sublistId: ‘inventoryassignment’,
fieldId: ‘issueinventorynumber’,
value:
line:
});
inventoryDetail.setSublistValue({
sublistId: ‘inventoryassignment’,
fieldId: ‘quantity’,
value:
line:
});
}
// Save the item fulfillment record
var fulfillmentId = itemFulfillmentRecord.save();
// Return the ID of the newly created item fulfillment
return fulfillmentId;
} catch (e) {
log.error({
title: ‘Error Creating Item Fulfillment’,
details: e
});
return null;
}
}
function reduce(context) {
// Gather information from processed invoices and send emails to customers
var ifId = context.key;
var ifData = context.values;
// Send email to customer
sendEmailToCustomer(ifId);
}
function sendEmailToCustomer(salesOrder) {
try {
// Retrieve customer email address from the sales order record
var customerEmail =’monika.phalle@theblueflamelabs.com’;
// Compose email
var subject = ‘Your Order is Fulfilled’;
var body = ‘Dear customer,\n\nYour order has been fulfilled.’;
// Send email
email.send({
author: -5, // Set the sender ID (-5 represents the current user)
recipients: customerEmail,
subject: subject,
body: body
});
} catch (e) {
log.error({
title: ‘Error Sending Email’,
details: e
});
}
}
function summarize(summary) {
// Log summary information
// log.audit(‘Usage units consumed: ‘ + summary.usage);
// log.audit(‘Number of queues: ‘ + summary.concurrency);
// log.audit(‘Number of yields: ‘ + summary.yields);
// Process errors
summary.output.iterator().each(function(key, value) {
if (value.error) {
log.error(‘Error Sending Email for Invoice ID ‘ + value.ifId + ‘: ‘ + value.errorMessage);
}
return true;
});
}
return {
getInputData: getInputData,
map: map,
reduce: reduce,
summarize: summarize
};
});



