Delete the Settlement record and its related records

Problem:

Delete the Settlement record and its related records for Rohego US.

Solution:

/**
 * @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 customrecord_celigo_amzio_settle_transSearchObj = search.create({
                    type: "customrecord_celigo_amzio_settle_trans",
                    filters:
                        [
                            ["created", "within", "13.01.2022 0:00", "20.01.2022 23:59"],
                            "AND",
                            ["custrecord_celigo_amzio_set_amz_account", "anyof", "3"]
                        ],
                    columns:
                        [
                            search.createColumn({name: "internalid", label: "Internal ID"}),
                            search.createColumn({
                                name: "name",
                                sort: search.Sort.ASC,
                                label: "Name"
                            }),
                            search.createColumn({name: "scriptid", label: "Script ID"}),
                            search.createColumn({
                                name: "custrecord_celigo_amzio_set_tran_type",
                                label: "Transaction Type"
                            }),
                            search.createColumn({name: "custrecord_celigo_amzio_set_order_id", label: "Order Id"}),
                            search.createColumn({
                                name: "custrecord_celigo_amzio_set_settlemnt_id",
                                label: "Settlement Id"
                            }),
                            search.createColumn({name: "custrecord_celigo_amzio_set_recon_status", label: "Status"}),
                            search.createColumn({
                                name: "custrecord_celigo_amzio_set_tran_sub_tot",
                                label: "Net Revenue"
                            }),
                            search.createColumn({
                                name: "custrecord_celigo_amzio_set_tran_amount",
                                label: "Transaction Amount"
                            })
                        ]
                });

                log.debug("search data", customrecord_celigo_amzio_settle_transSearchObj)

                return customrecord_celigo_amzio_settle_transSearchObj;

            } catch (err) {
                log.debug('error@getinput', err)
            }
        }


        /**
         * 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
         */

        function settlementFee(name) {

            try {
                var customrecord_celigo_amzio_sett_feeSearchObj = search.create({
                    type: "customrecord_celigo_amzio_sett_fee",
                    filters:
                        [
                            ["custrecord_celigo_amzio_set_f_par_trans", "anyof", name]
                        ],
                    columns:
                        [

                            search.createColumn({
                                name: "custrecord_celigo_amzio_set_f_par_trans",
                                label: "Parent Transaction"
                            }),
                            search.createColumn({name: "internalid", label: "Internal ID"})
                        ]
                });

                var searchResult = customrecord_celigo_amzio_sett_feeSearchObj.run();
                var result = searchResult.getRange({

                    start: 0,
                    end: 1000
                });
                log.debug("Item Fee result", result)

                // for( let i =0; i <result.length;i++)
                // {
                //     let delID;
                //     delID = result[0];
                //     log.debug('delID',delID)
                // }

                var arr = [];
                var obj;
                for (var i in result) {
                    obj = {};
                    obj.internalid = result[i].getValue(searchResult.columns[1]);
                    arr.push(obj);
                }
                //log.debug("delID",obj.internalid)
                log.debug("length", arr.length)
                for (let i = 0; i < arr.length; i++) {
                    log.debug("delID", arr[i])
                    record.delete({
                        id: arr[i].internalid,
                        type: 'customrecord_celigo_amzio_sett_fee'
                    });

                }

            } catch (err) {
                log.debug('error @ function1', err)
            }
        }

        function settelementITemPrice(val) {
            try {

                var customrecord_celigo_amzio_set_item_priceSearchObj = search.create({
                    type: "customrecord_celigo_amzio_set_item_price",
                    filters:
                        [
                            ["custrecord_celigo_amzio_set_ip_par_trans", "anyof", val]
                        ],
                    columns:
                        [

                            search.createColumn({
                                name: "custrecord_celigo_amzio_set_ip_or_it_id",
                                label: "Order Item Id"
                            }),
                            search.createColumn({
                                name: "custrecord_celigo_amzio_set_ip_par_trans",
                                label: "Parent Transaction"
                            }),
                            search.createColumn({name: "internalid", label: "Internal ID"})
                        ]
                });

                var itemPriceResult = customrecord_celigo_amzio_set_item_priceSearchObj.run();
                var itemResult = itemPriceResult.getRange({

                    start: 0,
                    end: 500

                });
                log.debug("Item Price result", itemResult)

                var arr = [];
                var obj;
                for (var i in itemResult) {
                    obj = {};
                    obj.internalid = itemResult[i].getValue(itemPriceResult.columns[1]);
                    arr.push(obj);
                }
                //log.debug("delID",obj.internalid)
                for (let i = 0; i < arr.length; i++) {
                    log.debug("Price delID", arr[i])
                    record.delete({
                        id: arr[i].internalid,
                        type: 'customrecord_celigo_amzio_set_item_price'
                    });

                }

            } catch (err) {
                log.debug(err)
            }
        }

        function abc(val) {

            try {

                var customrecord_celigo_amzio_set_item_priceSearchObj = search.create({
                    type: "customrecord_celigo_amzio_set_item_price",
                    filters:
                        [
                            ["custrecord_celigo_amzio_set_ip_par_trans", "anyof", val]
                        ],
                    columns:
                        [
                            search.createColumn({
                                name: "custrecord_celigo_amzio_set_ip_par_trans",
                                label: "Parent Transaction"
                            }),
                            search.createColumn({name: "internalid", label: "Internal ID"})
                        ]
                });

                // return customrecord_celigo_amzio_set_item_priceSearchObj;
                var itemPriceResult2 = customrecord_celigo_amzio_set_item_priceSearchObj.run();
                var itemResult2 = itemPriceResult2.getRange({

                    start: 0,
                    end: 500

                });
                log.debug("Item Price result2", itemResult2)

                var arr = [];
                var obj;
                for (var i in itemResult2) {
                    obj = {};
                    obj.internalid = itemResult2[i].getValue(itemPriceResult2.columns[1]);
                    arr.push(obj);
                }
                //log.debug("delID",obj.internalid)
                for (let i = 0; i < arr.length; i++) {
                    log.debug("Price delID 2", arr[i])
                    record.delete({
                        id: arr[i].internalid,
                        type: 'customrecord_celigo_amzio_set_item_price'
                    });

                }


            } catch (err) {
                log.debug("error@function", err)
            }
        }


        const reduce = (reduceContext) => {

            try {

                log.debug('In Reduce', reduceContext)
                let dataObj = JSON.parse(reduceContext.values)
                log.debug('recordData', dataObj)
                let recordID = dataObj.values.internalid.value
                settlementFee(recordID)
                settelementITemPrice(recordID)
                abc(recordID)
                record.delete({
                    id: recordID,
                    type: 'customrecord_celigo_amzio_settle_trans'
                });

            } catch (err) {
                log.debug('error@reduce', err)
            }

        }


        /**
         * 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
         */
        
 
        return {getInputData, reduce}

    });

Leave a comment

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