Hello, we have a programmer trying to push SO and PO's into netsuite, he has the SO's flowing but when he started working on the PO's he is getting an error. he is using a webhook.
Error he is getting - order responseNetSuite\Classes\AddResponse Object
(
[writeResponse] => NetSuite\Classes\WriteResponse Object
(
[status] => NetSuite\Classes\Status Object
(
[statusDetail] => Array
(
[0] => NetSuite\Classes\StatusDetail Object
(
[code] => UNEXPECTED_ERROR
[message] => An unexpected error occurred. Error ID: melx4vaifdvxzasvsg3m
[afterSubmitFailed] =>
[type] => ERROR
)
[1] => NetSuite\Classes\StatusDetail Object
(
[code] => WARNING
[message] => 50624317 : 50624317-000NC00010 Gildan - Heavy Cotton 100% Cotton T-Shirt. 5000-Ash-2XL: You have only 0 available for commitment across all locations (90 back ordered, 0 on order).
[afterSubmitFailed] =>
[type] => WARN
)
[2] => NetSuite\Classes\StatusDetail Object
(
[code] => WARNING
[message] => 50624317 : 50624317-000NC00010 Gildan - Heavy Cotton 100% Cotton T-Shirt. 5000-Ash-2XL: You have only 0 available for commitment at this location (90 back ordered, 0 on order).
[afterSubmitFailed] =>
[type] => WARN
)
[3] => NetSuite\Classes\StatusDetail Object
(
[code] => WARNING
[message] => Quantity not allowed for this item.
[afterSubmitFailed] =>
[type] => WARN
)
[4] => NetSuite\Classes\StatusDetail Object
(
[code] => WARNING
[message] => Rate not allowed for this item.
[afterSubmitFailed] =>
[type] => WARN
)
[5] => NetSuite\Classes\StatusDetail Object
(
[code] => WARNING
[message] => Amount not allowed for this item.
[afterSubmitFailed] =>
[type] => WARN
)
)
[isSuccess] =>
)
[baseRef] =>
)
)
Here is the full script he is using -
/**
- @NApiVersion 2.1
- let soId = context.newRecord.id;
let soRec = record.load({
type: record.Type.SALES_ORDER,
id: soId,
isDynamic: false
});
let trigger = soRec.getValue('custbody_auto_fulfill_trigger');
if (!trigger) {
log.debug('Skip', 'custbody_auto_fulfill_trigger not checked.');
return;
}
let location = soRec.getValue('location');
let lineCount = soRec.getLineCount({ sublistId: 'item' });
let vendorGroups = {};
for (let i = 0; i < lineCount; i++) {
let autoFulfill = soRec.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_auto_fulfill',
line: i
});
if (!autoFulfill) continue;
let itemId = soRec.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line: i
});
let qty = soRec.getSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: i
});
let rate = soRec.getSublistValue({
sublistId: 'item',
fieldId: 'rate',
line: i
});
// Load item to fetch preferred vendor
let itemRec = record.load({
type: record.Type.INVENTORY_ITEM,
id: itemId,
isDynamic: false
});
let vendorId = itemRec.getValue('preferredvendor');
if (!vendorId) {
log.debug('No Vendor', `Item ${itemId} has no preferred vendor`);
continue;
}
if (!vendorGroups[vendorId]) vendorGroups[vendorId] = [];
vendorGroups[vendorId].push({ itemId, qty, rate });
}
// Create POs per vendor
for (let vendorId in vendorGroups) {
let poRec = record.create({
type: record.Type.PURCHASE_ORDER,
isDynamic: true
});
poRec.setValue('entity', vendorId);
if (location) {
poRec.setValue('location', location);
}
// Link back to Sales Order
poRec.setValue('createdfrom', soId);
vendorGroups[vendorId].forEach(line => {
poRec.selectNewLine({ sublistId: 'item' });
poRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value: line.itemId
});
poRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: line.qty
});
poRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'rate',
value: line.rate
});
if (location) {
poRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'location',
value: location
});
}
poRec.commitLine({ sublistId: 'item' });
});
let poId = poRec.save();
log.audit('PO Created & Linked', `Vendor: ${vendorId}, PO ID: ${poId}, Linked SO: ${soId}`);
}
// ? Uncheck trigger on Sales Order so script doesn't rerun
record.submitFields({
type: record.Type.SALES_ORDER,
id: soId,
values: {
custbody_auto_fulfill_trigger: false
},
options: {
ignoreMandatoryFields: true
}
});
log.audit('SO Updated', `custbody_auto_fulfill_trigger unchecked on SO ${soId}`);
} catch (e) {
log.error('Error in afterSubmit', e);
}function afterSubmit(context) {
try {
if (context.type !== context.UserEventType.CREATE &&
context.type !== context.UserEventType.EDIT) {
return;
}}return { afterSubmit };
});