CodeMonkey
CodeMonkey

Reputation: 117

Pass Script Parameters

I'm having some trouble passing in Script Parameters for this code. The code doesn't seem to be reading in any values for the parameters from this execution log and error message: https://photos.app.goo.gl/FXKYkFAXMctP4WhY6

/** * @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 
    };

});

Upvotes: 0

Views: 1374

Answers (2)

Charl
Charl

Reputation: 827

Since your error code is "INVALID_RCRD_TRANSFORM", the problem is likely that your Sales Order cannot be transformed to an Invoice. This could be because your 'createdfrom' field on your record is not a Sales Order or the order is not in the correct status to be Invoiced.

Make sure that your createdFrom is indeed a Sales Order. Otherwise, your Sales Order might simply not be in the correct status to be invoiced. Have a look at Eric's answer on this question: Transform sales order to invoice error

Upvotes: 0

Jon Lamb
Jon Lamb

Reputation: 1473

Can you try passing an object into the getParameter function like this?

var freightItem = runtime.getCurrentScript().getParameter({ name: 'custscript_freight_item' });

Upvotes: 0

Related Questions