0

I am trying to create a Dropship Purchase Order via SuiteScript 2. I am able to create the Purchase order object with the correct Drop ship data (customer, Sales order, Dropship form) however I get the following error when saving

"You must enter at least one line item for this transaction.".

I can manually create the drop ship from the Sales Order and the items add fine. I am using Netsuite OneWorld.

Below is the code I'm using

var purchaseOrder = record.create({
                type: record.Type.PURCHASE_ORDER,
                isDynamic: true,
                defaultValues: {
                    soid: 4427821,
                    dropship: true,
                    subsidiary: 9,
                    custid: 666,
                    entity: 322
                }
});
purchaseOrder.setValue({
    fieldId: "employee",
    value: 3
});
    
log.debug("Item Count", purchaseOrder.getLineCount("item"));
log.debug("Entity", purchaseOrder.getText("entity"));
log.debug("Customer", purchaseOrder.getText("shipto"));
log.debug("Sales Order", purchaseOrder.getText("createdfrom"));
log.debug("Form", purchaseOrder.getText("customform"));
log.debug("Subsidiary", purchaseOrder.getText("subsidiary"));
    
purchaseOrder.save();

Here some screengrabs as well

Sales Order

Manual Drop Ship PO

Script Logs

I have existing scripts that create standalone POs, so I have some idea of the process required here. Is there a step I'm missing for Dropships specifically? I found this thread in which Will Charbonneau said this should be all you need Netsuite: How to link Purchase Order to Sales Order. I've tried Their code with my IDs, and it results in the same error.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Tyler
  • 1
  • 3

3 Answers3

1

The defaultValue object only accept entity value. Please look at the record.create defaultValue docs.

To Add your shipment item, you can do that by adding any other item to the item sublist.

var purchaseOrderRecordObj = record.create({
    type: record.Type.PURCHASE_ORDER,
    isDynamic: true,
    defaultValues: {
        entity: 322
    }
});

purchaseOrderRecordObj.setValue({ fieldId: 'createdfrom', value: 4427821, ignoreFieldChange: false }); //createdfrom no soid
purchaseOrderRecordObj.setValue({ fieldId: 'subsidiary', value: 9, ignoreFieldChange: false });
purchaseOrderRecordObj.setValue({ fieldId: 'memo', value: "Created from Sales Order as Dropship", ignoreFieldChange: false });    
purchaseOrderRecordObj.setValue({ fieldId: 'employee', value: 3, ignoreFieldChange: false });

purchaseOrderRecordObj.selectNewLine({ sublistId: 'item' });
purchaseOrderRecordObj.setCurrentSublistValue({ sublistId: 'item', fieldId: 'item', value: '<DROP_SHIP_ITEM_ID', ignoreFieldChange: false })
//if you want to change the quantity, or the price do here ... 
purchaseOrderRecordObj.commitLine({ sublistId: 'item', ignoreRecalc: false })

purchaseOrderRecordObj.save({ ignoreMandatoryFields: true });

Hope This helps.

Nadeem Khoury
  • 886
  • 6
  • 18
  • Hi Nadeem, thanks for the response. I tried without the customer and sales order defaultvalues and the purchase order isn't created as a dropship order. Regarding the line items, do you know how to link them to the Sales order? I've already tried adding items manually but it just adds lines as standalone items. If I Receive the Purchase order it doesn't dispatch the Sales order. – Tyler May 21 '23 at 23:46
0

I was able to reproduce the same error you had, and the code below shows the solution. This was tested successfully in the Script Debugger.

require(['N/record', 'N/log'],
function(record, log) {
    var purchaseOrder = record.create({
        type: record.Type.PURCHASE_ORDER,
        isDynamic: true,
        defaultValues: {
            soid: 1 /* random sales order */,
            dropship: true,
            subsidiary: 1 /* the one and only in my account */,
            custid: 4 /* Entity Internal ID for Customer: Powerslide */,
            entity: 10 /* Entity Internal ID for Vendor: Salesforce */
        }
    });

    purchaseOrder.setValue({
        fieldId: "employee",
        value: 3 /* me */
    });

    // -----
    // BEGIN: Add a line to the purchase order, so as to avoid the error,
    //        "You must enter at least one line item for this transaction"
    // -----
    purchaseOrder.selectNewLine({
        sublistId: 'item'
    });

    purchaseOrder.setCurrentSublistValue({
        sublistId: 'item',
        fieldId: 'item',
        value: 6 /* Non-inventory Item for Purchase */
    });

    purchaseOrder.setCurrentSublistValue({
        sublistId: 'item',
        fieldId: 'quantity',
        value: 1
    });

    purchaseOrder.setCurrentSublistValue({
        sublistId: 'item',
        fieldId: 'rate',
        value: 10000 /* casual $10k */
    });

    purchaseOrder.commitLine({sublistId: 'item'});
    // -----
    // END: Add a line to the purchase order
    // -----

    log.debug("Item Count", purchaseOrder.getLineCount("item"));
    log.debug("Entity", purchaseOrder.getText("entity"));
    log.debug("Customer", purchaseOrder.getText("shipto"));
    log.debug("Sales Order", purchaseOrder.getText("createdfrom"));
    log.debug("Form", purchaseOrder.getText("customform"));
    log.debug("Subsidiary", purchaseOrder.getText("subsidiary"));

    purchaseOrder.save();
});

It seems the key steps are as follows.

  1. Use Record.selectNewLine() to start constructing a new line.
  2. Use Record.setCurrentSublistValue() to populate fields on the new line.
  3. Use Record.commitLine() when you are done populating fields.

The key parts of this solution were pulled from another answer. Just giving credit where credit is due.

Marty C.
  • 576
  • 2
  • 7
  • 22
  • Hi Marty, thanks for the response. Do you know how to link the lines to the Sales order? I thought of that at first but it just adds lines as standalone items. If I Receive the Purchase order it doesn't dispatch the Sales order. – Tyler May 21 '23 at 23:44
  • Great question, Tyler, and I'm learning more about NetSuite as we go here. [It seems](https://stackoverflow.com/a/72661997) NetSuite may only allow you to "link" a PO to an SO through a sales order line item through a configuration value _when the **sales order** line item is created_. So, that means the approach we're taking here doesn't actually do what you want. What you want is to implement business logic applied to the _sales order_ to set [`createpo`](https://www.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2021_1/script/record/salesorder.html) so as to generate a linked PO. – Marty C. May 25 '23 at 03:53
  • Hi Marty, ive done some testing based on the link you provided. please see my findings below: 1. items that can be manually dropshipped already (via UI) had the value "Dropship" in the createpo field. Setting this does nothing. https://snipboard.io/kNuEId.jpg 2. I tried manually creating a PO and assigning both the id and reference to the createpo field. This doesn't link the transaction either. – Tyler Jun 02 '23 at 07:24
  • I imagine that's really frustrating. Thank you for the update, Tyler. Any luck logging a bug with NetSuite Support about this problem? That's what I've done in similar situations in the past. – Marty C. Jun 07 '23 at 00:45
  • Hi Marty, that was my next thing to do. I was really hoping someone had the solution and it was just me doing something stupid. – Tyler Jun 12 '23 at 01:43
0

So in case anyone is still having trouble with this, I figured it out.

var purchaseOrder = record.create({
            type: record.Type.PURCHASE_ORDER,
            isDynamic: true,
            defaultValues: {
                customform: 180, //dropship custom form
                soid: 4820583, //Sales order internalID
                shipgroup: 1, //from 'dropship' button url via UI
                dropship: true, //from 'dropship' button url via the UI
                custid: 666, //customer InternalID
                entity: 322, //vendor InternalID
                poentity: 322 //vendor InternalID
            }
        });
var poid = purchaseOrder.save();

I don't know why half of these fields are not documented anywhere (defaultvalues or the schema) but this auto populates the items and customer details just like it would via the UI.

Tyler
  • 1
  • 3