Delete Customer Address if it is Not Default

Jira Code: AHD-82

Delete all Seller Addresses that are not set to Default Billing or Default Shipping.

/**
 * @NApiVersion 2.x
 * @NScriptType MapReduceScript
 * @NModuleScope SameAccount
 */
/**

* Script Description

* This Map Reduce to Delete Seller address
* 
*/

/*******************************************************************************
 * 
 * Support Files
 * 
 * *****************************************************************************
 * 
 * 
 * 
 * $Author: Jobin & Jismi IT Services LLP $
 * 
 * DESCRIPTION
 * 
 * Delete Seller address
 * 
 * 
 ******************************************************************************/
define(['N/record', 'N/file', 'N/email', 'N/runtime', 'N/search'],

    function (record, file, email, runtime, search) {
        var main = {
            getInputData: function () {
                var customerSearchObj = search.create({
                    type: "customer",
                    filters: [
                        ["isdefaultbilling", "is", "F"],
                        "AND",
                        ["isdefaultshipping", "is", "F"],
                        "AND",
                        ["status", "anyof", "13", "16", "15"]
                    ],
                    columns: [
                        search.createColumn({
                            name: "addressinternalid",
                            label: "Address Internal ID"
                        })
                    ]
                });
                return customerSearchObj;
            },

            reduce: function (context) {
                log.debug("context", context.values)
                var valueId = context.values
                log.debug("valueId", valueId[0]["recordType"])
                var seller = record.load({
                    type: record.Type.CUSTOMER,
                    id: context.key
                });
                try {
                    context.values.forEach(function (value) {
                        value = JSON.parse(value);
                        log.debug("value", value)
                        var addressId = value["values"]["addressinternalid"];
                        log.debug("itemId", addressId)
                        var lineNumber = seller.findSublistLineWithValue({
                            sublistId: 'addressbook',
                            fieldId: 'internalid',
                            value: addressId
                        });
                        seller.removeLine({
                            sublistId: 'addressbook',
                            line: lineNumber,
                            ignoreRecalc: true
                        });
                    })
                    seller.save();
                } catch (error) {
                    log.debug("error", error);
                }
            },
            summarize: function (summary) {},
        };
        for (var key in main) {
            if (typeof main[key] === 'function') {
                main[key] = trycatch(main[key], key);
            }
        }

        function trycatch(myfunction, key) {
            return function () {
                try {
                    return myfunction.apply(this, arguments);
                } catch (e) {
                    log.debug("e in  " + key, e);
                }
            }
        };
        return main;
    });

Leave a comment

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