Import of customer deposit csv through script

/**

 * @NApiVersion 2.1

 * @NScriptType ScheduledScript

 */

define([‘N/record’, ‘N/file’, ‘N/format’, ‘N/search’],

    /**

 * @param{record} record

 * @param{file} file

 */

    (record, file, format, search) => {

        function findCustId(customerName) {

            let customerSearchObj = search.create({

                type: “customer”,

                filters:

                    [

                        [“companyname”, “is”, customerName]

                    ],

                columns:

                    [

                        search.createColumn({ name: “internalid”, label: “Internal ID” }),

                       

                    ]

            });

            log.debug(“customer search”,customerSearchObj);

           

            let searchResultCount = customerSearchObj.runPaged().count;

            log.debug(“customerSearchObj result count”, searchResultCount);

            let custId;

            customerSearchObj.run().each(function (result) {

                custId = result.getValue({

                    name: “internalid”,

                    label: “Internal ID”

                });

                log.debug(“customer id in search”, custId);

               

                return false;

            });

         

            return custId;

        }

        /**

         * Defines the Scheduled script trigger point.

         * @param {Object} scriptContext

         * @param {string} scriptContext.type – Script execution context. Use values from the scriptContext.InvocationType enum.

         * @since 2015.2

         */

        const execute = (scriptContext) => {

            // Load the CSV file from the file cabinet

            let fileObj = file.load({

                id: 12831 // Internal ID of the CSV file in the file cabinet

            });

            let fileContent = fileObj.getContents();

            // Split the content of the file into an array of lines

           

            let arrLines = fileContent.split(/n|nr/);

            log.debug(“file contents”, fileContent);

            log.debug(“array of lines”, arrLines);

            for (let i = 1; i < arrLines.length 1; i++) {

               

                let line = arrLines[i].split(‘,’);

                let transactionId = line[0].replace(/^|$/g, );

                let date = line[1].replace(/^|$/g, );

                let transactionType = line[2].replace(/^|$/g, );

                let customerName = line[3].replace(/^|$/g, );

                let memoDescription = line[5].replace(/^|$/g, );

                let accountFullName = line[6].replace(/^|$/g, );

                let debit = line[7].replace(/^|$/g, );

                let location = line[8].replace(/^|$/g, );

                let clazz = line[9].replace(/^|$/g, );

                let customerID = findCustId(customerName);

                let parsedDate = format.parse({

                    value: date,

                    type: format.Type.DATE

                });

                log.debug(“customer”, customerID);

                let custDepo = record.create({

                    type: record.Type.CUSTOMER_DEPOSIT,

                    isDynamic: true

                });

                custDepo.setValue({

                    fieldId: ‘trandate’,

                    value: parsedDate

                });

                custDepo.setValue({

                    fieldId: ‘memo’,

                    value: memoDescription

                });

                custDepo.setValue({

                    fieldId: ‘customer’,

                    value: customerID

                });

                custDepo.setValue({

                    fieldId: ‘payment’,

                    value: Number(debit)

                });

                custDepo.setText({

                    fieldId: ‘location’,

                    value: location

                });

                custDepo.setText({

                    fieldId: ‘class’,

                    value: clazz

                });

                custDepo.setText({

                    fieldId: ‘account’,

                    value: accountFullName

                });

               

                let depositId = custDepo.save({ignoreMandatoryFields: false});

               

                log.debug(‘Customer Deposit Created’, ‘Customer Deposit ID: ‘ + depositId);

            }

        }

        return { execute }

    });

Leave a comment

Your email address will not be published. Required fields are marked *