Script to Update payment card token

Description:

MR script that runs every day, searches for payment card tokens created on that day and load & save the line records (custom record) which holds the MASK value in the payment card Lookup Key field (custom field).

/**
 * @NApiVersion 2.1
 * @NScriptType MapReduceScript
 * @NModuleScope SameAccount


 * *******************************************************************************************

define(['N/record', 'N/search', './moment.js', './vv_lib_constants.js', '/SuiteScripts/corrdyn/CustomModules/vv_lib_autoship_display_name.js', './vv_new_lib_constants.js'],

    /**
     * @param {record} record
     * @param {search} search
     */
    function (record, search, moment, _lib_constants, _lib_autoship, _libfile) {
        const _SEARCH_ID = "customsearch_vv_system_notes_pct"

        /* CUSTOM RECORD IDS */
        const _AUTOSHIP_MASTER_RECORD = "customrecord_vv_autoship_customer";
        const _AUTOSHIP_LINE_RECORD = "customrecord_vv_autoship_record";
        /* CUSTOM RECORD IDS */

        /* CUSTOM RECORD IDS */
        const _AUTOSHIP_PYMT_CARD_TOKEN = "custrecord_vv_payment_card_token";
        const _AUTOSHIP_CC_PYMT_TOKEN_LOOKUP = "custrecord_vv_autoship_selected_card";
        const _AUTOSHIP_CUSTOMER = "custrecord_vv_autoship_customer_src";
        /* CUSTOM RECORD IDS */

        function getInputData() {
            const GLOBAL_CONSTANTS = _libfile.getConstants()
            const _PCT_TYPE = GLOBAL_CONSTANTS.PAYMENT_INSTRUMENTS.PAYMENT_CARD_TOKEN; // "-539"

            var pctIdArray = []
            var systemnoteSearchObj = search.create({
                type: "systemnote",
                filters:
                    [
                        ["recordtype", "anyof", _PCT_TYPE],
                        "AND",
                        [["type", "is", "T"], "AND", ["date", "on", "today"]]
                    ],
                columns:
                    [
                        search.createColumn({
                            name: "recordid",
                            summary: "GROUP",
                            label: "Record ID"
                        })
                    ]
            });
            var searchResultCount = systemnoteSearchObj.runPaged().count;
            log.debug("systemnoteSearchObj result count", searchResultCount);
            systemnoteSearchObj.run().each(function (result) {
                var val = result.getValue(systemnoteSearchObj.columns[0]);
                pctIdArray = pctIdArray.concat(val)
                return true;
            });
            log.debug("pctIdArray", pctIdArray)

            return pctIdArray;
        }

        /**
         * Executes when the reduce entry point is triggered and applies to each group.
         * @param {ReduceSummary} context - Data collection containing the groups to process through the reduce stage
         * @since 2015.1
         */
        function reduce(context) {
            var pctIdVal = context.values[0];
            log.debug("pctIdVal", pctIdVal);
            try {
                if (pctIdVal) {
                    var pctLookupField = search.lookupFields({
                        type: search.Type.PAYMENT_INSTRUMENT,
                        id: pctIdVal,
                        columns: ['mask', 'entity']
                    });
                    log.debug("pctLookupField", pctLookupField)

                    if (pctLookupField.hasOwnProperty("mask")) {
                        var pctMask = pctLookupField.mask;
                        log.debug("pctMask", pctMask);

                        var pctCustomer = pctLookupField.entity[0].value
                        log.debug("pctCustomer", pctCustomer);

                        var autoshipRecordSearchObj = search.create({
                            type: _AUTOSHIP_LINE_RECORD,
                            filters:
                                [
                                    [_AUTOSHIP_CC_PYMT_TOKEN_LOOKUP, "is", pctMask],
                                    "AND",
                                    [_AUTOSHIP_CUSTOMER, "anyof", pctCustomer]
                                ],
                            columns:
                                [
                                    search.createColumn({name: "internalid", label: "Internal ID"})
                                ]
                        });
                        var searchResultCount = autoshipRecordSearchObj.runPaged().count;
                        log.debug("autoshipRecordSearchObj result count", searchResultCount);

                        var lineRecId;
                        autoshipRecordSearchObj.run().each(function (result) {
                            lineRecId = result.getValue(autoshipRecordSearchObj.columns[0]);

                            if (lineRecId) {
                                log.debug("lineRecId", lineRecId)
                                record.submitFields({
                                    type: _AUTOSHIP_LINE_RECORD,
                                    id: lineRecId,
                                    values: {
                                        [_AUTOSHIP_PYMT_CARD_TOKEN]: pctIdVal
                                    },
                                    options: {
                                        enablesourcing: true,
                                        ignoreMandatoryFields: true
                                    }
                                });
                            }
                            return true;
                        });

                    }
                }

            } catch (e) {
                log.debug("ENTERED ERROR REDUCE", e)
                log.error("ENTERED ERROR REDUCE", e)
            }
        }

        function summarize(summary) {
            log.debug("EXECUTION FINISHED", "TIME ELAPSED = " + (summary.seconds / 60) + " min(s)");
        }

        return {
            getInputData: getInputData,
            reduce: reduce,
            summarize: summarize
        }
    }
);

Leave a comment

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