Modifying an old customers script and the followin...
# suitescript
n
Modifying an old customers script and the following is not returning anything for amount or quantitybilled and so the lines are not setting on the invoice
Copy code
var invoice = nlapiTransformRecord('salesorder', rentalIds[y], 'invoice');
        var invCount = invoice.getLineItemCount('item');
        var soCustomer = invoice.getFieldValue('entity')

        if (!invCount) {
            continue;
        }

        for (var z = 0; z < invCount; z++) {
            var amount = invoice.getLineItemValue('item', 'amount', z);
            var quantity = invoice.getLineItemValue('item', 'quantity', z);
            
            
            nlapiLogExecution('DEBUG', 'quantitybilled', invoice.getLineItemValue('item', 'quantitybilled', z));
            nlapiLogExecution('DEBUG', 'amount', amount);
            if (invoice.getLineItemValue('item', 'quantity', z)) {
                invoice.setLineItemValue('item', 'quantity', z, 1);
            }

            if (invoice.getLineItemValue('item', 'amount', z)) {
                invoice.setLineItemValue('item', 'amount', z, amount);
            }
        }
        var newInvId = nlapiSubmitRecord(invoice);
b
i wouldnt touch that script if it wasnt yours
Copy code
for (var z = 0; z < invCount; z++) {
is not how you correctly loop in ss1
n
Then how would you fix this in 1.0?
b
im saying that there is a fair chance the script never worked correctly
fixing the script means writing it for the first time
n
So, i rewrote it in 2.0 and here is the issue @battk. I have a sales order with item quantity 999. The first time I do record.transform (setting all quantities to 1) it works. But when I try again it tells me there are no amounts set
b
and your new code?
n
Copy code
/**
*@NApiVersion 2.x
*@NScriptType ScheduledScript
*/
define(['N/search', 'N/record', 'N/email', 'N/runtime'],
    function (search, record, email, runtime) {
        function execute(context) {
            var salesOrderRec = '';

            var salesorderSearchObj = search.create({
                type: "salesorder",
                filters:
                    [
                        ["type", "anyof", "SalesOrd"],
                        "AND",
                        ["mainline", "is", "T"],
                        "AND",
                        ["customform", "anyof", "312"],
                        "AND",
                        ["status", "noneof", "SalesOrd:G", "SalesOrd:C", "SalesOrd:H"],
                        "AND",
                        ["custbody_dc_rental_billing_int", "noneof", "5"],
                        "AND",
                        ["custbody_dc_next_bill_date", "on", "today"]
                    ],
                columns:
                    [
                        search.createColumn({ name: "internalid", label: "Internal ID" })
                    ]
            });
            salesorderSearchObj.run().each(function (result) {
                // .run().each has a limit of 4,000 results
                salesOrderRec = result.id
                return true;
            });

            if (salesOrderRec) {
                var sorec = record.transform({
                    fromType: 'salesorder',
                    fromId: salesOrderRec,
                    toType: 'invoice',
                    isDynamic: false,
                });

                var numLines = sorec.getLineCount({
                    sublistId: 'item'
                });

                var customer = sorec.getValue({
                    fieldId: 'entity'
                });

                for (var i = 0; i < numLines; i++) {
                    var amount = sorec.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'amount',
                        line: i
                    });
                    var quantityBilled = sorec.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'quantitybilled',
                        line: i
                    });
                    var rentalclass = sorec.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'custcol_nco_rental_class',
                        line: i
                    });

                    var prorate = sorec.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'custcol_dc_rental_aggr_prorate_days',
                        line: i
                    });

                    sorec.setSublistValue({
                        sublistId: 'item',
                        fieldId: 'quantity',
                        line: i,
                        value: 1
                    });

                    if (rentalclass) {
                        var equipmentrate = getRentalEquipmentRate(rentalclass, customer);

                        sorec.setSublistValue({
                            sublistId: 'item',
                            fieldId: 'rate',
                            line: i,
                            value: equipmentrate
                        });

                        sorec.setSublistValue({
                            sublistId: 'item',
                            fieldId: 'amount',
                            line: i,
                            value: (equipmentrate / 28) * prorate
                        });
                    } else {
                        sorec.setSublistValue({
                            sublistId: 'item',
                            fieldId: 'amount',
                            line: i,
                            value: amount
                        });
                    }
                }

                var invrec = sorec.save({
                    enableSourcing: true,
                    ignoreMandatoryFields: true
                });

                log.debug({ title: 'invrec', details: invrec });
            }
        }

        function getRentalEquipmentRate(rentalclass, customer) {
            var rates = [];

            try {

                var customrecord_nco_unit_rental_profileSearchObj = search.create({
                    type: "customrecord_nco_unit_rental_profile",
                    filters:
                        [
                            ["custrecord_nco_rental_class", "anyof", rentalclass]
                        ],
                    columns:
                        [
                            search.createColumn({ name: "custrecord_nco_rental_customer", label: "Rental Customer" }),
                            search.createColumn({ name: "custrecord_nco_rental_rate", label: "Rate" })
                        ]
                });
                customrecord_nco_unit_rental_profileSearchObj.run().each(function (result) {
                    // .run().each has a limit of 4,000 results
                    rates.push({
                        customer: result.getValue({ name: 'custrecord_nco_rental_customer' }),
                        rate: result.getValue({ name: 'custrecord_nco_rental_rate' })
                    })
                    return true;
                });

            } catch (ex) {
                log.error({ title: 'getRentalEquipmentRate', details: ex.message });
            }

            var customerRate = 0;

            if (rates.length > 0) {
                customerRate = rates[0].rate;

                for (var i = 0; i < rates.length; i++) {
                    if (rates[i].customer == customer) {
                        customerRate = rates[i].rate * 1
                    }
                }
            }


            return customerRate
        }

        return {
            execute: execute
        };
    });
b
and the reasoning behind the logic, its pretty weird to try and bill someone multiple times above and beyond the amount of the sales order
n
its part of a rental setup. Custom work
b
you probably want to take a look at the recurring billing related solutions
otherwise
Copy code
salesorderSearchObj.run().each(function (result) {
                // .run().each has a limit of 4,000 results
                salesOrderRec = result.id
                return true;
            });
is not how you usually do searches
n
how else would you search?
b
your code is discarding every search result and keeping the last one
n
well there is only one result for now while i am testing
b
eventually you will have to change how you do your search
until then, you should share your error message
especially the stack trace