I want a purchase order to be generated when a person clicks the approve button on a sales order. I have the script deployed to sales order records and the event type to trigger when the approve button is clicked. However, this code won't create a purchase order because i have an invalid field value for the sublist item value.
I've used both the internal Id and the string name of the item as values and i get the same "invalid field value" error. Anyone know what's wrong?
function beforeSubmit(context) {
var sRecord = context.newRecord;
var user = runtime.getCurrentUser();
//get line count
var itemCount = sRecord.getLineCount({
sublistId: 'item'
});
for (var i = 0; i<itemCount; i++){
var pOrder = record.create({
type: record.Type.PURCHASE_ORDER,
isDynamic: true
});
//get item internal id
var itemId = sRecord.getSublistValue({
sublistId : 'item',
fieldId : 'item',
line : i
});
//get qty
var qty = sRecord.getSublistValue({
sublistId : 'item',
fieldId : 'quantity',
line : i
});
//get vendor of item
var vendor = search.lookupFields({
type : 'item',
id : itemId,
columns : ['vendorname']
});
//add vendor to record
pOrder.setValue('vendorname', vendor);
//selects new line
pOrder.selectNewLine({sublistId: 'item'});
//add item to sublist
pOrder.setCurrentSublistValue({
sublistId : 'item',
fieldId : 'item',
value : itemId
});
//add quantity to sublist
pOrder.setCurrentSublistValue({
sublistId : 'item',
fieldId : 'quantity',
value : qty
});
pOrder.commitLine({sublistId: 'item'});
pOrder.save();
}
}
The entity
field is required on the purchase order record. The vendorname
field on the item record does not store a vendor record, and even if it did, there isn't a corresponding vendorname
field on the PO. This value from the item record is used to identify the vendor's name/code for the item itself, in the event they have a different name.
As written, this code functions at least situationally if the entity
field is set on the PO. If you've set a Preferred Vendor
or if using the multiple vendors feature have configured a preferred vendor there, you could lookup the appropriate entity
field directly from the Item record still.
An easy way to achieve what this script appears to be doing without scripting for Inventory and Non-inventory for sale items is to use the Drop Ship Item
checkbox or the Special Order Item
checkbox on these item records. Selecting either for each eligible item will allow automatic creation of po's for the line items of your Sales Order. Search Special Order Items
in the Netsuite Help for more information on the differences and how to setup either.