HEllo Every one ,
I am facing issues while itemfulfilemt of the salesorder when we have muitple line items.
We are getting below error while trying to tensform the sale order to item fulfilment .
It is able to fulfilt the first item of the sales order but throwing the error while fulfiling the second time and so on.
Also, it give error when we try to fulfil any randowm item for the sales order as it say''VALID lijne item is required or SSS_INVALID_SUBLIST_OPERATION.
Below is our scripts
/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
*/
define(['N/record', 'N/search', 'N/log'],
function (record, search, log) {
function getInputData() {
var customRecordSearch = search.
create({
type: 'test\_trx\_item\_shipment\_json',
filters
: \[\[
'test\_record\_status', 'is', '2'
\]\],
columns: \['testrecord\_json\_payload', 'internalid'\]
});
log.debug({ 'customRecordSearch': customRecordSearch });
return customRecordSearch;
}
function map(context) {
log.debug({ 'Map context': context });
var searchResult = JSON.parse(context.value);
// log.debug({ 'Search Result': searchResult });
log.debug({
title: 'Search Result (Stringified)',
details: JSON.stringify(searchResult, null, 2)
});
log.debug({ 'searchResult': searchResult.id });
var jsonPayload = JSON.parse(searchResult.values.custrecord\_record\_json\_payload);
log.debug({ 'JSON Payload': jsonPayload });
var salesOrderSearch = search.create({
type: search.Type.SALES\_ORDER,
filters: \[
\['test\_trx\_oms\_orderid', 'is', 'J4683234924'\],
\],
columns: \['internalid'\]
});
var salesOrderResult = salesOrderSearch.run().getRange(0, 1);
var salesOrderId = salesOrderResult\[0\].id;
var itemFulfillment = record.transform({
fromType: record.Type.SALES\_ORDER,
fromId: salesOrderId,
toType: record.Type.ITEM\_FULFILLMENT,
isDynamic: true,
defaultValues: {
inventorylocation: 202
}
});
// Loop through Shipments array
jsonPayload.Shipments.Shipment.forEach(function (shipment) {
var jobID = shipment.JobID;
var trackingNumber = shipment.TrackingNo;
var mfgLocation = shipment.mfgLocation;
var sample = shipment.Sample;
var shipToCompany = shipment.shipToCompany;
var ShipmentDate = shipment.ShipmentDate;
log.debug('Processing Shipment', {
jobID: jobID,
trackingNumber: trackingNumber
});
// Process Sales Order
updateSalesOrder(jobID, trackingNumber, ShipmentDate, mfgLocation, sample, shipToCompany, searchResult,itemFulfillment);
});
log.debug('Item Fulfillment created', itemFulfillment);
var fulfillmentId = itemFulfillment.save();
log.debug('Item Fulfillment saved', fulfillmentId);
}
function updateSalesOrder(jobID, trackingNumber, ShipmentDate, mfgLocation, sample, shipToCompany, searchResult,itemFulfillment) {
try {
var externalOrderID = jobID.split('-')\[0\];
var salesOrderSearch = search.create({
type: search.Type.SALES\_ORDER,
filters: \[
\['trx\_oms\_orderid', 'is', externalOrderID\],
\],
columns: \['internalid'\]
});
var salesOrderResult = salesOrderSearch.run().getRange(0, 1);
log.debug({
title: 'sales Order Search Result (Stringified)',
details: JSON.stringify(salesOrderResult, null, 2)
});
if (salesOrderResult.length > 0) {
var salesOrderId = salesOrderResult\[0\].id;
log.debug('sales Order Id', salesOrderId);
var salesOrder = record.load({
type: record.Type.SALES\_ORDER,
id: salesOrderId
});
var salesOrderStatus = salesOrder.getValue('status');
if (!(salesOrderStatus === 'Pending Fulfillment' || salesOrderStatus === 'Partially Fulfilled' || salesOrderStatus === 'Pending Billing/Partially Fulfilled') ){
log.error('Sales Order not ready for fulfillment', salesOrderStatus);
return;
}
log.debug('salesOrderStatus ', salesOrderStatus);
//searchLineItem();
// var inventorylocation = salesOrder.getSublistValue({
// sublistId: 'item',
// fieldId: 'location',
// line: i
// });
// var itemFulfillment = record.transform({
// fromType: record.Type.SALES\_ORDER,
// fromId: salesOrderId,
// toType: record.Type.ITEM\_FULFILLMENT,
// isDynamic: true,
// defaultValues: {
// inventorylocation: 202
// }
// });
var lineItemCount = salesOrder.getLineCount({ sublistId: 'item' });
log.debug('Line Item Count', lineItemCount);
for (var i = 0; i \< lineItemCount; i++) {
var itemJobId = salesOrder.getSublistValue({
sublistId: 'item',
fieldId: 'test\_item\_jobid',
line: i
});
// var inventorylocation = salesOrder.getSublistValue({
// sublistId: 'item',
// fieldId: 'location',
// line: i
// });
//log.debug('iinventorylocation', inventorylocation);
log.debug('item Job ID', itemJobId);
log.debug(' Job ID', jobID);
if (itemJobId === jobID) {
// var itemFulfillment = record.transform({
// fromType: record.Type.SALES\_ORDER,
// fromId: salesOrderId,
// toType: record.Type.ITEM\_FULFILLMENT,
// isDynamic: true,
// defaultValues: {
// inventorylocation: inventorylocation
// }
// });
// log.debug('itemFulfillment Record Initilized', itemFulfillment);//
// log.debug('need to select the new line ')
//itemFulfillment.selectNewLine({ sublistId: 'item',line: i });
// log.debug('New Line Selected ')
itemFulfillment.selectLine({ sublistId: 'item', line: i });
// Check if the item is fulfillable
var fulfillable = itemFulfillment.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'itemreceive'
});
log.debug('Item fulfillable', fulfillable);
if (fulfillable) {
itemFulfillment.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: itemFulfillment.getSublistValue({
sublistId: 'item',
fieldId: 'quantityremaining',
line: i
})
});
itemFulfillment.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'test\_item\_mfg\_location',
value: mfgLocation,
line: i
});
itemFulfillment.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'test\_item\_sample',
value: sample,
line: i
});
itemFulfillment.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'testitem\_ship\_date',
value: ShipmentDate,
line: i
});
itemFulfillment.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'test\_item\_shipto\_company',
value: shipToCompany,
line: i
});
itemFulfillment.commitLine({ sublistId: 'item' });
log.debug('setting Ship Status');
itemFulfillment.setValue({ fieldId: 'shipstatus', value: 'C' });
if (trackingNumber) {
log.debug('Item Fulfillment trackingNumber', trackingNumber);
itemFulfillment.selectNewLine({ sublistId: 'package' });
itemFulfillment.setCurrentSublistValue({ sublistId: 'package', fieldId: 'packageweight', value: 1 });
itemFulfillment.setCurrentSublistValue({ sublistId: 'package', fieldId: 'packagetrackingnumber', value: trackingNumber });
itemFulfillment.commitLine({ sublistId: 'package' });
}
log.debug('Item Fulfillment line item non inventory', itemFulfillment);
}
break;
}
}
log.debug('Item Fulfillment created', itemFulfillment);
var fulfillmentId = itemFulfillment.save();
log.debug('Item Fulfillment saved', fulfillmentId);
// var invocie\_create = record.transform({
// fromType: record.Type.SALES\_ORDER,
// fromId: salesOrderId,
// toType: record.Type.INVOICE,
// isDynamic: true
// });
// var save\_invoice = invocie\_create.save();
log.debug('save\_invoice saved', save\_invoice);
//updateShipmentStatus(searchResult, 3, 'Shipment procress Successfully');
} else {
log.error('Shipmemnt not found', 'No Shipment order found for JobID: ' + jobID);
//updateShipmentStatus(searchResult, 4, 'Shipmemnt not found');
}
} catch (e) {
log.error('Shipmemnt not found', e);
// updateShipmentStatus(searchResult, 4, "Error updating Shipment");
}
}
function searchLineItem(salesOrderId, jobID) {
log.debug('searchLineItem Called', { salesOrderId: salesOrderId, jobID: jobID });
// Perform a search on the Sales Order record type for line items matching the given jobID
var lineItemSearch = search.create({
type: 'salesorder',
filters: \[
\['internalid', 'is', salesOrderId\], // Match the Sales Order by internal ID
'AND',
\['item.test\_item\_jobid', 'is', jobID\] // Match the job ID on the item line
\],
columns: \[
'item', // Retrieve item internal ID
'item.test\_item\_jobid' // Optionally include the custom field column
\]
});
var searchResults = lineItemSearch.run().getRange({ start: 0, end: 1 });
if (searchResults.length > 0) {
var itemId = searchResults\[0\].getValue('item');
log.debug('Found Item ID', itemId);
return itemId;
} else {
log.debug('No matching line item found');
return null;
}
}
function updateShipmentStatus(searchResult, status, errorMessage) {
log.debug('updateShipmentStatus Called');
try {
var customShipmentRecords = record.load({
type: 'trx\_item\_shipment\_json',
id: searchResult.id,
});
log.debug('updateShipmentStatus customShipmentRecords Called', customShipmentRecords);
customShipmentRecords.setValue({ fieldId: 'test', value: status });
customShipmentRecords.setValue({ fieldId: 'test', value: errorMessage });
customShipmentRecords.save();
} catch (e) {
log.error("error while updating Sales order" + e.message);
}
}
function reduce() {
}
return {
getInputData: getInputData,
map: map,
reduce: reduce,
};
});