2

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();

    }
}
Chris Lim
  • 56
  • 8
  • or is there a way to trigger the "create special order" for each line item in a sales order when the approve button is clicked? – Chris Lim Nov 11 '16 at 07:05
  • I'm assuming that you have tried setting one of those items on a PO through the UI as well, to make sure that it is available for POs? – w3bguy Nov 11 '16 at 13:32
  • @W3BGUY yes creating a PO through the UI works perfectly fine. the error only occurs at the point where i am adding an item – Chris Lim Nov 12 '16 at 18:52

1 Answers1

3

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.

Shea Brennan
  • 1,132
  • 7
  • 16
  • I see. so i am assuming that because the entity field isn't set, it can't find any associated items for the purchase order (because you need to set a vendor before adding items). Is that right? edit: i can't believe that was the problem. although i havent re-deployed the script, i think that was the problem. thanks so much! – Chris Lim Nov 12 '16 at 18:53