1

I'm having an error when I try to transform a newly created sales order to an invoice.

Here's my code:

  define(["N/log", "N/email", "N/record"], function(log, email, record) {

    function afterSubmit(scriptContext) {

        var newOrderId = scriptContext.newRecord.id;

        var objRecord = record.transform({
            fromType: record.Type.SALES_ORDER,
            fromId: newOrderId,
            toType: record.Type.INVOICE,
            isDynamic: true
        });

Here's the error details:

Account: 3671783
Environment: SandBox
Date & Time: 6/28/2017 2:09 am
Record Type: Sales Order
Internal ID: 668978
Execution Time: 0.00s
Script Usage: 0
Script: test send invoice
Type: User Event
Function: afterSubmit
Error: JS_EXCEPTION
{"type":"error.SuiteScriptError","name":"INVALID_INITIALIZE_REF","message":"You can not initialize invoice: invalid reference 668978.","stack":["createError(N/error.js)","afterSubmit(/SuiteScripts/sendinvoice1.js:23)"],"cause":{"type":"internal error","code":"INVALID_INITIALIZE_REF","details":"You can not initialize invoice: invalid reference 668978.","userEvent":"aftersubmit","stackTrace":["createError(N/error.js)","afterSubmit(/SuiteScripts/sendinvoice1.js:23)"],"notifyOff":false},"id":"","notifyOff":false}

I don't know what am I doing wrong.

mana
  • 1,075
  • 1
  • 24
  • 43

1 Answers1

8

What status is the Sales Order in when you're trying to transform it? Before you can Invoice an SO, there are some criteria that must be met under certain conditions:

  • If you have Approvals on for Sales Orders, then your SO must be Approved before you can Invoice it.
  • If any of the Items on the SO are fulfillable, then the Order must be fulfilled (i.e. have an associated Item Fulfillment) before you can Invoice it
    • UNLESS you have the INVOICE IN ADVANCE OF FULFILLMENT setting enabled (Setup > Accounting > Accounting Preferences > Order Management > Invoicing), then you can forego the Item Fulfillment
erictgrubaugh
  • 8,519
  • 1
  • 20
  • 28