Reputation: 11
I have a suite script (RESTlet): transform Transfer Order into Item Fulfillment on NetSuite
/**
* This SuiteScript RESTlet transforms a Transfer Order into an Item Fulfillment.
*
* u/NApiVersion 2.x
* u/NScriptType Restlet
* u/NModuleScope SameAccount
*/
define(['N/record'], function(record) {
function createItemFulfillment(data) {
var transferOrderId = data.transferOrderId;
var locationId = data.locationId; // Location ID for the Item Fulfillment
var lineItems = data.lineItems;
log.debug('data', data)
// Load the Transfer Order
var transferOrder = record.load({
type: record.Type.TRANSFER_ORDER,
id: transferOrderId,
isDynamic: true
});
var trandate = transferOrder.getValue('trandate');
var memo = transferOrder.getValue('memo');
log.debug('trandate', trandate)
log.debug('memo', memo)
// Create an Item Fulfillment
var itemFulfillment = record.transform({
fromType: 'transferorder',
fromId: transferOrderId,
toType: 'itemfulfillment',
isDynamic: true
});
var fulfill = itemFulfillment.
log.debug('itemFulfillment', itemFulfillment)
// Set the location on the Item Fulfillment
// itemFulfillment.setValue('location', locationId);
itemFulfillment.setValue({ fieldId: 'trandate', value: trandate }); //new Date()
// Loop through Transfer Order lines and copy them to Item Fulfillment
var lineItemCount = transferOrder.getLineCount({ sublistId: 'item' });
// Loop through Transfer Order line items and copy them to Item Fulfillment
for (var i = 0; i < lineItemCount; i++) {
try {
var itemId = transferOrder.getSublistValue({ sublistId: 'item', fieldId: 'item', line: i });
var quantity = transferOrder.getSublistValue({ sublistId: 'item', fieldId: 'quantity', line: i });
log.debug('item', itemId)
log.debug('quantity', quantity)
itemFulfillment.selectNewLine({ sublistId: 'item' });
itemFulfillment.setCurrentSublistValue({ sublistId: 'item', fieldId: 'item', value: itemId });
itemFulfillment.setCurrentSublistValue({ sublistId: 'item', fieldId: 'quantity', value: quantity });
// You can set more sublist fields if needed
itemFulfillment.commitLine({ sublistId: 'item' });
} catch (ex){
log.error('Sublist Operation Error', ex.message);
return { success: false, errorMessage: ex.message };
}
}
// Save the Item Fulfillment
var fulfillmentId = itemFulfillment.save();
return fulfillmentId;
}
function doPost(context) {
if (context) { // Check if there is a request object
var requestData = JSON.parse(JSON.stringify(context)); // Parse the request body
var response = createItemFulfillment(requestData);
return response;
} else {
return { error: 'abcxyz' };
}
}
return {
post: doPost
};
});
on Postman, at requestBody I set a value transferOrderID.
Then, I receive an error:
SSS_INVALID_SUBLIST_OPERATION: You have attempted an invalid sublist or line item operation. You are either trying to access a field on a non-existent line or you are trying to add or remove lines from a static sublist.
I tried log.debug and get the value of Transfer Order is okay, but I could not set the value for Item Fulfillment .
Can someone help me? I really appreciate your support efforts.
I tried log.debug and get the value of Transfer Order is okay, but I could not set the value for Item Fulfillment .
Upvotes: 1
Views: 564
Reputation: 5276
When you create (transform) an item fulfillment from a transfer order, it is created with all the lines corresponding to fulfillable lines on the order. You cannot add lines, as you are attempting to do with itemFulfillment.selectNewLine({ sublistId: 'item' });
.
You can observe this if you fulfill an order in the UI - there is no option to add lines, as there is on an order, for example. You can also use itemFulfillment.getLineCount('item')
immediately after record.transform()
to verify that the lines already exist.
Use itemFulfillment.selectLine()
(instead of selectNewLine()
) if you need to make changes to the values in the item lines. Be aware that you can only set the same values you would be able to in the UI - for example if your settings restrict fulfillment based on item commitment, you won't be able to set a quantity to fulfill that's greater than the committed quantity - a scenario that may arise with the code in your example.
Upvotes: 1