Delete customers and remove the address

Jira Code: AHD-135

Delete the customers based on the different criteria using the mass update script. There might be related records like phone calls, task, event, sales order, invoice, item fulfilment, etc.

Also removing the address lines from the customer record where the default shipping and default billing are not set.

/**
 * @NApiVersion 2.x
 * @NScriptType MassUpdateScript
 * @NModuleScope SameAccount
 * @ScriptTitle AHD 131 MU Delete Sellers.js
 * @AppliedTo Customer
 * @Description This script is used to delete all customer based on the criteria
 */
/*******************************************************************************
 * AHD 
 * **************************************************************************
 * Date: 08/07/2018
 * 
 * Author: Jobin & Jismi IT Services LLP
 * 
 * 
 * REVISION HISTORY :
 *
 * 
 ******************************************************************************/
define(['N/record', 'N/search'],
/**
 * @param {record} record
 * @param {search} search
 */
function(record, search) {
    
    /**
     * Definition of Mass Update trigger point.
     *
     * @param {Object} params
     * @param {string} params.type - Record type of the record being processed by the mass update
     * @param {number} params.id - ID of the record being processed by the mass update
     *
     * @since 2016.1
     */
    function each(params) {
    	try{
    	//to get the id 
    	var recId=params.id;
    	// to delete the record
    	record.delete({
    	type:'customer',
    	id:recId
    	});
    	}catch(e)
    	{
    		log.error('err@ deletion',e.message);
    	}
    }

    return {
        each: each
    };
    
});
/**
 * @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",
                        ["stage", "anyof", "CUSTOMER"]
                    ],
                    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
                });
                //if (valueId.length > 2) {
                    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 *