Reputation: 33
I've been trying to create a Suitescript to fulfill a Transfer Order but have had no luck. I'm not sure if I need to create both an item fulfillment and an item receipt? I seem to get different answers everywhere I look.
I need the script to fulfill quantities of each item on the Transfer Order and update its status to "Received".
The script that I have so far was able to create an item fulfillment. This updated the "complete" quantity on the transfer order items but not the fulfilled quantity. The status also remains in "Pending Fulfillment".
Any help would be greatly appreciated.
Here's what I've been testing in the script debugger so far:
require(['N/record', 'N/search', 'N/log'], function(record, search, log) {
try {
var transferOrderId = 533010;
var transferOrder = record.load({
type: record.Type.TRANSFER_ORDER,
id: transferOrderId
});
var transferOrderType = transferOrder.type;
// Ensure the record being processed is a Transfer Order
if (transferOrderType !== 'transferorder') {
log.debug('Not a Transfer Order', 'Skipping processing.');
return;
}
// ********** Item Fulfillment Logic **********
// Check if an Item Fulfillment already exists for the Transfer Order
var fulfillmentSearch = search.create({
type: record.Type.ITEM_FULFILLMENT,
filters: [
['createdfrom', 'is', transferOrderId]
],
columns: ['internalid']
});
var fulfillmentResults = fulfillmentSearch.run().getRange({ start: 0, end: 1 });
if (fulfillmentResults && fulfillmentResults.length > 0) {
log.debug('Item Fulfillment Exists', 'Item Fulfillment already exists for Transfer Order ID: ' + transferOrderId);
} else {
// Transform the Transfer Order to an Item Fulfillment
var itemFulfillment = record.transform({
fromType: record.Type.TRANSFER_ORDER,
fromId: transferOrderId,
toType: record.Type.ITEM_FULFILLMENT,
isDynamic: true
});
// Iterate over the lines to ensure quantities are properly set
var fulfillmentLineCount = itemFulfillment.getLineCount({ sublistId: 'item' });
for (var i = 0; i < fulfillmentLineCount; i++) {
itemFulfillment.selectLine({ sublistId: 'item', line: i });
// Set the quantity to fulfill as the quantity remaining to be fulfilled
var quantityToFulfill = itemFulfillment.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantityremaining'
});
itemFulfillment.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: quantityToFulfill
});
itemFulfillment.commitLine({ sublistId: 'item' });
}
// Save the Item Fulfillment
var itemFulfillmentId = itemFulfillment.save();
log.debug('Item Fulfillment Created', 'Item Fulfillment ID: ' + itemFulfillmentId);
}
// ********** Item Receipt Logic **********
// Check if an Item Receipt already exists for the Transfer Order
var receiptSearch = search.create({
type: record.Type.ITEM_RECEIPT,
filters: [
['createdfrom', 'is', transferOrderId]
],
columns: ['internalid']
});
var receiptResults = receiptSearch.run().getRange({ start: 0, end: 1 });
if (receiptResults && receiptResults.length > 0) {
log.debug('Item Receipt Exists', 'Item Receipt already exists for Transfer Order ID: ' + transferOrderId);
} else {
// Transform the Transfer Order to an Item Receipt
var itemReceipt = record.transform({
fromType: record.Type.TRANSFER_ORDER,
fromId: transferOrderId,
toType: record.Type.ITEM_RECEIPT,
isDynamic: true
});
// Iterate over the lines to ensure quantities are properly set
var receiptLineCount = itemReceipt.getLineCount({ sublistId: 'item' });
for (var i = 0; i < receiptLineCount; i++) {
itemReceipt.selectLine({ sublistId: 'item', line: i });
// Set the quantity to receive as the quantity remaining to be received
var quantityRemaining = itemReceipt.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantityremaining'
});
itemReceipt.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: quantityRemaining
});
itemReceipt.commitLine({ sublistId: 'item' });
}
// Save the Item Receipt
var itemReceiptId = itemReceipt.save();
log.debug('Item Receipt Created', 'Item Receipt ID: ' + itemReceiptId);
}
} catch (e) {
log.error('Error Processing Transfer Order', e);
}
});
Upvotes: 0
Views: 79
Reputation: 453
You have the right idea, in general. When working with Transfer Orders, you need to Fulfill it before you can Receive it. That's why you get the Invalid Reference error on creation of the Receipt.
As for the Fulfillment, you said you're not seeing the items on the TO as Fulfilled. Does your account have Pick-Pack-Shipped statuses for Fulfillments? I think in your transform for the Fulfillment you just need to set the Status to Shipped. Then do the lines, as you are, and save. See if that helps.
Example:
itemFulfillment.setValue({name: 'status', value:'C'});
Upvotes: 0