Journal Entry Line Delete

Client Needs To Delete The Lines From The Journal Entry Record

/**
 * @NApiVersion 2.1
 * @NScriptType MapReduceScript
 */
define(['N/record', 'N/search'],
    /**
 * @param{record} record
 * @param{search} search
 */
    (record, search) => {
        /**
         * Defines the function that is executed at the beginning of the map/reduce process and generates the input data.
         * @param {Object} inputContext
         * @param {boolean} inputContext.isRestarted - Indicates whether the current invocation of this function is the first
         *     invocation (if true, the current invocation is not the first invocation and this function has been restarted)
         * @param {Object} inputContext.ObjectRef - Object that references the input data
         * @typedef {Object} ObjectRef
         * @property {string|number} ObjectRef.id - Internal ID of the record instance that contains the input data
         * @property {string} ObjectRef.type - Type of the record instance that contains the input data
         * @returns {Array|Object|Search|ObjectRef|File|Query} The input data to use in the map/reduce process
         * @since 2015.2
         */

        const getInputData = (inputContext) => {
            try{
                var journalentrySearchObj = search.create({
                type: "journalentry",
                filters:
                [
                   ["type","anyof","Journal"], 
                   "AND", 
                   ["memo","contains","payable to amazon"],
                //    "AND",
                //    ["internalid","anyof","5740723"]
                ],
                columns:
                [
                   search.createColumn({
                      name: "ordertype",
                      sort: search.Sort.ASC,
                      label: "Order Type"
                   }),
                   search.createColumn({name: "mainline", label: "*"}),
                   search.createColumn({name: "trandate", label: "Date"}),
                   search.createColumn({name: "asofdate", label: "As-Of Date"}),
                   search.createColumn({name: "postingperiod", label: "Period"}),
                   search.createColumn({name: "taxperiod", label: "Tax Period"}),
                   search.createColumn({name: "type", label: "Type"}),
                   search.createColumn({name: "tranid", label: "Document Number"}),
                   search.createColumn({name: "entity", label: "Name"}),
                   search.createColumn({name: "account", label: "Account"}),
                   search.createColumn({name: "memo", label: "Memo"}),
                   search.createColumn({name: "amount", label: "Amount"}),
                   search.createColumn({name: "custbody_4599_mx_operation_type", label: "MX Operation Type"}),
                   search.createColumn({name: "custbody_4599_sg_import_permit_num", label: "Import Permit No."}),
                   search.createColumn({name: "custbody_my_import_declaration_num", label: "Import Declaration No."}),
                   search.createColumn({name: "line.cseg1", label: "Marketplace"}),
                   search.createColumn({name: "line.cseg2", label: "Geography"}),
                   search.createColumn({name: "custbody_11187_pref_entity_bank", label: "Preferred Entity Bank"}),
                   search.createColumn({name: "custbody_11724_pay_bank_fees", label: "Vendor Bank Fees"}),
                   search.createColumn({name: "custbody_11724_bank_fee", label: "Bank Fee"}),
                   search.createColumn({name: "custbody_15529_vendor_entity_bank", label: "Entity Bank (Vendor)"}),
                   search.createColumn({name: "custbody_15529_emp_entity_bank", label: "Entity Bank (Employee)"}),
                   search.createColumn({name: "custbody_15699_exclude_from_ep_process", label: "Exclude from Electronic Bank Payments Processing"}),
                   search.createColumn({name: "custbody_rf_journal_category", label: "Journal Category"}),
                   search.createColumn({name: "custbody_rf_po_pur_reason", label: "Purchase Reason"}),
                   search.createColumn({name: "custbody_rf_po_delivery_instructions", label: "Delivery Instructions"}),
                   search.createColumn({name: "custbody_rf_so_full_payment_received", label: "Fulfillment Allowed"}),
                   search.createColumn({name: "custbody_rf_bank_name", label: "Bank Name"}),
                   search.createColumn({name: "custbody_rf_subsidiary_information", label: "Subsidiary Information"}),
                   search.createColumn({name: "custbodyrf_bank_account_no", label: "Bank Account_no"}),
                   search.createColumn({name: "custbody_rf_swift_code", label: "Swift Code"}),
                   search.createColumn({name: "custbody_rf_address_line1", label: "Address Line1"}),
                   search.createColumn({name: "custbody_rf_address_line2", label: "Address Line2"}),
                   search.createColumn({name: "custbody_rf_country", label: "Country"}),
                   search.createColumn({name: "custbody_rf_state", label: "State"}),
                   search.createColumn({name: "custbody_rf_currency", label: "Currency"}),
                   search.createColumn({name: "custbody_rf_ach_routing_no", label: "ACH ROUTING NO"}),
                   search.createColumn({name: "custbody_rf_account_name", label: "Account Name"}),
                   search.createColumn({name: "custbody_rf_pro_inv_sale_rep_email", label: "RF Sales Rep Email"}),
                   search.createColumn({name: "custbody_rf_subsidiary_log_proforma", label: "Subsidiary Logo for Proforma Invoice"}),
                   search.createColumn({name: "custbody_rf_so_customer_category", label: "Customer Category"}),
                   search.createColumn({name: "custbody_rf_transaction_created_by", label: "Transaction CreatedBy"}),
                   search.createColumn({name: "custbody_rf_transaction_approver", label: "Transaction Approver"}),
                   search.createColumn({name: "custbody_10184_customer_entity_bank", label: "Entity Bank (Customer)"}),
                   search.createColumn({name: "custbody2", label: "Deletion Helper"}),
                   search.createColumn({name: "custbody4", label: "Shipment Mode"}),
                   search.createColumn({name: "custbody5", label: "DELETE ME"}),
                   search.createColumn({name: "custbody_15889_cust_refund_entity_bank", label: "Entity Bank (Customer Credit)"}),
                   search.createColumn({name: "custbodycust_tct_planner", label: "Requested By"}),
                   search.createColumn({name: "cseg1", label: "Marketplace"}),
                   search.createColumn({name: "cseg2", label: "Geography"})
                ]
             });
             return journalentrySearchObj;
            }
            catch(e){
                log.debug("error@getInputData",e)
            }
        }

        /**
         * Defines the function that is executed when the map entry point is triggered. This entry point is triggered automatically
         * when the associated getInputData stage is complete. This function is applied to each key-value pair in the provided
         * context.
         * @param {Object} mapContext - Data collection containing the key-value pairs to process in the map stage. This parameter
         *     is provided automatically based on the results of the getInputData stage.
         * @param {Iterator} mapContext.errors - Serialized errors that were thrown during previous attempts to execute the map
         *     function on the current key-value pair
         * @param {number} mapContext.executionNo - Number of times the map function has been executed on the current key-value
         *     pair
         * @param {boolean} mapContext.isRestarted - Indicates whether the current invocation of this function is the first
         *     invocation (if true, the current invocation is not the first invocation and this function has been restarted)
         * @param {string} mapContext.key - Key to be processed during the map stage
         * @param {string} mapContext.value - Value to be processed during the map stage
         * @since 2015.2
         */

        // const map = (mapContext) => {

        // }

        /**
         * Defines the function that is executed when the reduce entry point is triggered. This entry point is triggered
         * automatically when the associated map stage is complete. This function is applied to each group in the provided context.
         * @param {Object} reduceContext - Data collection containing the groups to process in the reduce stage. This parameter is
         *     provided automatically based on the results of the map stage.
         * @param {Iterator} reduceContext.errors - Serialized errors that were thrown during previous attempts to execute the
         *     reduce function on the current group
         * @param {number} reduceContext.executionNo - Number of times the reduce function has been executed on the current group
         * @param {boolean} reduceContext.isRestarted - Indicates whether the current invocation of this function is the first
         *     invocation (if true, the current invocation is not the first invocation and this function has been restarted)
         * @param {string} reduceContext.key - Key to be processed during the reduce stage
         * @param {List<String>} reduceContext.values - All values associated with a unique key that was passed to the reduce stage
         *     for processing
         * @since 2015.2
         */
        const reduce = (reduceContext) => {
            try{
                let searchResult=reduceContext.values.map(JSON.parse);
                log.debug("searchResult",searchResult);
                let internalId=searchResult[0].id;
                log.debug("internalId",internalId)
                let currentRec=record.load({
                    type: record.Type.JOURNAL_ENTRY,
                    id: internalId,
                    isDynamic: false
                });
                log.debug("currentRec",currentRec)
                let lineCount=currentRec.getLineCount({
                    sublistId: 'line'
                });
                log.debug("lineCount",lineCount)
                for(let i=lineCount; i>0; i--){
                    let memo = currentRec.getSublistValue({
                        sublistId : 'line',
                        fieldId: 'memo',
                        line: i-1
                    });
                    log.debug("memo",memo);

                    if(memo && (memo.toString()).search('Payable to Amazon')>0){
                        let account=currentRec.getSublistValue({
                            sublistId : 'line',
                            fieldId: 'account',
                            line: i-1
                        });
                        log.debug("account",account);
                        for(let j=0; j<searchResult.length;j++){
                            let searchAccount=searchResult[j].values.account.value
                            if(account == searchAccount){
                                log.debug("account "+account + " searchAccount "+searchAccount)
                                currentRec.removeLine({
                                    sublistId: 'line',
                                    line: i-1,
                                    ignoreRecalc: true
                                });
                            }
                        }
                    }
                    // for(let j=0; j<searchResult.length;j++){
                    //     let searchMemo= searchResult[j].values.memo
                    //     log.debug("searchMemo",searchMemo)
                    //     if(memo==searchMemo){
                    //         let account=currentRec.getSublistValue({
                    //             sublistId : 'line',
                    //             fieldId: 'account',
                    //             line: lineCount
                    //         });
                    //         let searchAccount=searchResult[j].values.account.value
                    //         log.debug("Account","Record Account = "+account + "Search Account = "+searchAccount)
                    //     }
                    // }
                }
                currentRec.save()

            }
            catch(e){
                log.debug("error@reduce",e)
            }
        }


        /**
         * Defines the function that is executed when the summarize entry point is triggered. This entry point is triggered
         * automatically when the associated reduce stage is complete. This function is applied to the entire result set.
         * @param {Object} summaryContext - Statistics about the execution of a map/reduce script
         * @param {number} summaryContext.concurrency - Maximum concurrency number when executing parallel tasks for the map/reduce
         *     script
         * @param {Date} summaryContext.dateCreated - The date and time when the map/reduce script began running
         * @param {boolean} summaryContext.isRestarted - Indicates whether the current invocation of this function is the first
         *     invocation (if true, the current invocation is not the first invocation and this function has been restarted)
         * @param {Iterator} summaryContext.output - Serialized keys and values that were saved as output during the reduce stage
         * @param {number} summaryContext.seconds - Total seconds elapsed when running the map/reduce script
         * @param {number} summaryContext.usage - Total number of governance usage units consumed when running the map/reduce
         *     script
         * @param {number} summaryContext.yields - Total number of yields when running the map/reduce script
         * @param {Object} summaryContext.inputSummary - Statistics about the input stage
         * @param {Object} summaryContext.mapSummary - Statistics about the map stage
         * @param {Object} summaryContext.reduceSummary - Statistics about the reduce stage
         * @since 2015.2
         */
        const summarize = (summaryContext) => {

        }

        return {getInputData, reduce, summarize}

    });

Leave a comment

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