-1

I'm having a problem passing script parameters in a user event script. I know why my code is failing. Is there any way to pass text through parameters?

here is the error:

{"type":"error.SuiteScriptError","name":"INVALID_INITIALIZE_REF","message":"You can not initialize invoice: invalid reference 18349.","stack":["createError(N/error)","afterSubmit(/SuiteScripts/complexInvoice.js:29)"],"cause":{"type":"internal error","code":"INVALID_INITIALIZE_REF","details":"You can not initialize invoice: invalid reference 18349.","userEvent":"aftersubmit","stackTrace":["createError(N/error)","afterSubmit(/SuiteScripts/complexInvoice.js:29)"],"notifyOff":false},"id":"","notifyOff":false}

and here's the code:

/** * @NApiVersion 2.x * @NScriptType UserEventScript * @NModuleScope SameAccount */ define(["N/record", "N/log", "N/runtime"], function (record, log, runtime) {

function afterSubmit(context) {

    // Gather your variables
    var newRec = context.newRecord;
    var freightCost = newRec.getValue({
        fieldId: 'custbody_freight_cost'
    });
    var freightItem = runtime.getCurrentScript().getParameter('custscript_freight_item');
    var handlingItem = runtime.getCurrentScript().getParameter('custscript_handling_item');
    var salesOrderId = newRec.getValue({
        fieldId: 'createdfrom'
    });
    log.debug('Sales Order ID', salesOrderId);
    log.error({
        title: 'Freight Cost',
        details: freightCost
    });
    log.error({
        title: 'Freight Item',
        details: freightItem
    });

    // Transform the Sales Order into an Invoice
    var invoiceRecord = record.transform({
        fromType: record.Type.SALES_ORDER,
        fromId: salesOrderId,
        toType: record.Type.INVOICE,
        isDynamic: true
    });
    log.error({
        title: 'Debug Entry',
        details: invoiceRecord
    });
    invoiceRecord.selectNewLine({
        sublistId: 'item'
    });
    invoiceRecord.setCurrentSublistText({
        sublistId: 'item',
        fieldId: 'item',
        text: freightItem
    });
    invoiceRecord.setCurrentSublistValue({
        sublistId: 'item',
        fieldId: 'amount',
        value: freightCost
    });
    invoiceRecord.commitLine({
        sublistId: 'item'
    });
    invoiceRecord.selectNewLine({
        sublistId: 'item'
    });
    invoiceRecord.setCurrentSublistText({
        sublistId: 'item',
        fieldId: 'item',
        text: handlingItem
    });
    invoiceRecord.commitLine({
        sublistId: 'item'
    });

    // Here is how you set a body field
    invoiceRecord.setValue({
        fieldId: 'custbody_freight_cost',
        value: freightCost,
        ignoreFieldChange: true
    });

    // Submit the record
    var rid = invoiceRecord.save();
    log.debug('Saved Record', rid);
}
return {
    afterSubmit: afterSubmit 
    };

});

CodeMonkey
  • 97
  • 2
  • 9

2 Answers2

0

Likely your Sales Order 18349 is in an inappropriate status to be invoiced, e.g. Pending Approval.

erictgrubaugh
  • 8,519
  • 1
  • 20
  • 28
0

Check once it may works.

To fix this navigate to: (1) Setup > Accounting > Preferences > Set Up Accounting > Order Management tab > Invoicing (2) Enable the Invoice in Advance of Fulfillment preference (3) Save the Preference

After saving the preference, the above sample script should be able to transform Sales Order.