Autoship: Scheduled 2 pm process

Script 1: Creates Sales Order for auto-ship(custom) record. Script is powered by search which pulls all line records/auto-ship that are created inside the 2 day window ranga

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


 */

define( [ 'N/record', 'N/search', './moment.js' ],

    /**
     * @param {record} record
     * @param {search} search
     */
    function ( record, search, moment )
    {

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

        /* SO RECORD FIELDS */
        const _SO_ITEM = "item";
        const _SO_DISCOUNT_ITEM = "custcol_autoship_line_discount_item";
        const _SO_RATE = "rate";
        const _SO_DATE = "trandate"
        const _SO_AMOUNT = "amount";
        const _SO_QUANTITY = "quantity";
        const _SO_INTERVAL = "custcol_vv_so_line_autoship_interval";
        const _SO_FREQUENCY = "custcol_vv_so_line_autoship_frequency";
        const _SO_IS_RX_ITEM = "custcol_vv_is_rx_item";
        const _SO_VET_AUTH = "custcol_vvs_vetauth";
        const _SO_AUTOSHIP_ITEM = "custcol_vv_so_line_autoship_item";
        const _SO_AUTOSHIP_CREATED = "custcol_vv_so_line_autoship_created";
        const _SO_PRESCRIPTION = "custcol_vv_so_prescription";
        const _SO_VET = "custcol_vv_so_vet";
        const _SO_PET = "custcol_vv_so_pet";
        const _SO_LINE_UNIQUE_KEY = "lineuniquekey";
        const _SO_INITIAL_AUTOSHIP_LINE = "custcol_vv_so_initial_autoship_line";
        const _SO_DATE_PRESC_LINKED = "custcol_vv_ue_date_linked";
        const _SO_LINKED_AUTO_SHIP_LINE = "custcol_vv_linked_autoship_line"; //added per task 176811172
        const _SO_PRESCRIPTION_ITEM = "custcol_vv_prescription_item_id"; //added per task 176811172
        const _SO_INSTRUCTIONS = "custbody_vv_webstore_instructions.CUSTRECORD_VV_AUTOSHIP_SOREF"; //added per task 176099397
        const _SO_INITIAL_SHIP_METHOD = "custcol_vv_ship_method_initial"
        const _SO_PAYMENT_OPTION = "paymentoption"
        const _SO_STATUS = "orderstatus"
        const _SO_CATALOG_TYE = "custbody_catalog_name"
        const _SO_ORDER_TYPE = "custbody_sales_attribution"
        //const _SO_DISCOUNT_ITEM = "item.CUSTRECORD_VV_AUTOSHIP_SOREF"; //added per task 176099397

        const _SO_IS_AUTOSHIP_GENERATED = 'custbody_autoshipgenerated' //INTRODUCED FLAGGING IN FIELD SO TO UNDERSTAND THE SO IS CREATED FROM AUTOSHIP, for story https://www.pivotaltracker.com/story/show/181989624
        /* SO RECORD FIELDS */

        /* CUSTOM RECORD FIELDS */
        const _CR_AUTOSHIP_MASTER_CUSTOMER = "custrecord_vv_autoship_customer";
        const _CR_ITEM = "custrecord_vv_autoship_item"
        const _CR_DISCOUNT_ITEM = "custrecord_vv_discountitem"
        const _CR_QUANTITY = "custrecord_vv_autoship_quantity"
        const _CR_FREQUENCY = "custrecord_vv_autoship_freq"
        const _CR_RATE = "custrecord_vv_autoship_rate"
        const _CR_AMOUNT = "custrecord_vv_autoship_amount"
        const _CR_INTERVAL = "custrecord_vv_autoship_interval"
        const _CR_TERMINATE = "custrecord_vv_autoship_terminate"
        const _CR_RX_ITEM = "custrecord_vv_autoship_rxitem"
        const _CR_AUTOSHIP_CUSTOMER = "custrecord_vv_autoship_customer_id"
        const _CR_SO_REFERENCE = "custrecord_vv_autoship_soref"
        const _CR_ADD_ON = "custrecord_vv_autoship_addon"
        const _CR_AUTO_SHIP_DATE = "custrecord_vv_autoship_date"
        const _CR_NEXT_SHIP_DATE = "custrecord_vv_autoship_nextshipdate"
        const _CR_PET = "custrecord_vv_autoship_pet"
        const _CR_PRESCRIPTION = "custrecord_vv_autoship_prescription"
        const _CR_VET = "custrecord_vv_autoship_vet"
        const _CR_VET_AUTH = "custrecord_vv_autoship_vetauthmaster"
        const _CR_RX_ORDER_STATUS = "custrecord_vv_autoship_rxorderstatus"
        const _CR_LINE_UNIQUE_KEY = "custrecord_vv_autoship_line_unique_key";
        const _CR_IS_AUTOSHIP_ITEM = "custrecord_vv_is_autoship_item";
        const _CR_SHIPPING_ADDRESS = "custrecord_vv_autoship_shipping_address";
        const _CR_PAYMENT_CARD_TOKEN = "custrecord_vv_payment_card_token";
        /* CUSTOM RECORD FIELDS */

        /* PRESCRITION RECORD FIELDS OBJ*/
        const _PRESCRIPTION = {
            prId: "custrecord_vv_prescription_id",
            itemName: "custrecord_vv_item_name"
        }
        /* PRESCRITION RECORD FIELDS OBJ*/

        const _APPROVED = "2";
        const _UPS_GROUND = "263746"
        const _PENDING_FULFILLMENT = 'B'
        const _ORDER_TYPE_WEB = 3
        const _CATALOG_TYPE_WEB = 4

        function getInputData ()
        {
            return search.load( { id: "customsearch_vv_autoship_2pm" } ) // SEARCH: VVS Autoship Date Test_2pm (**DO NOT DELETE - USED IN SCRIPT**).
        }

        /**
         * Executes when the map entry point is triggered and applies to each key/value pair.
         * @param {MapSummary} context - Data collection containing the key/value pairs to process through the map stage
         * @since 2015.1
         */
        function map ( context )
        {
            try
            {
                log.debug( "Context Value", JSON.stringify( JSON.parse( context.value ) ) );

                var contextVal = JSON.parse( context.value );

                var objTemp = {
                    strAutoShipLineId: context.key,
                    strItem: contextVal.values[ _CR_ITEM ].value,
                    strDscntItem: contextVal.values[ _CR_DISCOUNT_ITEM ].value,
                    flQuantity: parseFloat( contextVal.values[ _CR_QUANTITY ] ) || 0,
                    intFrequency: parseInt( contextVal.values[ _CR_FREQUENCY ] ),
                    strInterval: contextVal.values[ _CR_INTERVAL ].value,
                    boolAddOn: contextVal.values[ _CR_ADD_ON ] == "F" ? false : true,
                    dtTrandate: contextVal.values[ _CR_NEXT_SHIP_DATE ],
                    strVet: contextVal.values[ _CR_VET ].value,
                    strPet: contextVal.values[ _CR_PET ].value,
                    strVetAuth: contextVal.values[ _CR_VET_AUTH ].value,
                    boolRXItem: contextVal.values[ _CR_RX_ITEM ] == "F" ? false : true,
                    boolIsAutoShip: contextVal.values[ _CR_IS_AUTOSHIP_ITEM ] == "F" ? false : true,
                    strSORef: contextVal.values[ _CR_SO_REFERENCE ].value || null,
                    strPrescriptionId: contextVal.values[ _CR_PRESCRIPTION ].value || null,
                    strShippingAddress: contextVal.values[ _CR_SHIPPING_ADDRESS ].value || null,
                    strInstructions: contextVal.values[ _SO_INSTRUCTIONS ],
                    dtAutoTrandate: contextVal.values[ _CR_AUTO_SHIP_DATE ],
                    stRate: contextVal.values[ _CR_RATE ],
                    paymentToken: contextVal.values[ _CR_PAYMENT_CARD_TOKEN ].value || null
                };

                log.debug( "objTemp", JSON.stringify( objTemp ) );

                context.write( contextVal.values[ "custrecord_vv_autoship_customer.CUSTRECORD_VV_AUTOSHIP_CUSTOMER_ID" ].value, objTemp )
            } catch ( e )
            {
                log.debug( "Err", e )
                log.error( "Err", e )
            }

        }

        /**
         * 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 )
        {
            log.debug( "Customer ID = " + context.key );
            try
            {
                log.debug( "ENTER createSalesOrderAndUpdateAutoShipLineRecord" )
                createSalesOrderAndUpdateAutoShipLineRecord( context );

            } catch ( e )
            {
                log.debug( "ENTERED ERROR REDUCE", e )
                log.error( "ENTERED ERROR REDUCE", e );
                try
                {
                    createSalesOrderAndUpdateAutoShipLineRecord( context );

                }
                catch ( err )
                {
                    log.debug( "Error in reduce", err )
                    log.error( "Error in reduce", err )
                }

            }
        }

        function createSalesOrderAndUpdateAutoShipLineRecord ( context )
        {
            try
            {
                var objAutoShipsByShipAddress = {};
                //group the SOs with different shipping addresses
                for ( var index = 0; index < context.values.length; index++ )
                {
                    var objData = JSON.parse( context.values[ index ] );

                    if ( !objAutoShipsByShipAddress.hasOwnProperty( objData.strShippingAddress ) )
                    {
                        log.debug( "objData1", objData )
                        // log.debug( "objData1.strShippingAddress", objData.strShippingAddress )
                        objAutoShipsByShipAddress[ objData.strShippingAddress ] = [];
                    }
                    log.debug( "objData2", objData )
                    //log.debug( "objData2.strShippingAddress", objData.strShippingAddress )
                    objAutoShipsByShipAddress[ objData.strShippingAddress ].push( objData );
                }

                for ( var itr in objAutoShipsByShipAddress )
                {
                    log.debug( "itr", itr )
                    var stSalesOrderRec = record.create( { type: record.Type.SALES_ORDER, defaultValues: { entity: context.key } } );
                    stSalesOrderRec.setValue( { fieldId: "shipaddresslist", value: itr } );

                    //INTRODUCED FLAGGING IN FIELD SO TO UNDERSTAND THE SO IS CREATED FROM AUTOSHIP

                    stSalesOrderRec.setValue( { fieldId: _SO_IS_AUTOSHIP_GENERATED, value: true } );
                    stSalesOrderRec.setValue( { fieldId: _SO_STATUS, value: _PENDING_FULFILLMENT } );
                    stSalesOrderRec.setValue( { fieldId: _SO_CATALOG_TYE, value: _CATALOG_TYPE_WEB } );
                    stSalesOrderRec.setValue( { fieldId: _SO_ORDER_TYPE, value: _ORDER_TYPE_WEB } );

                    var arrAutoShips = objAutoShipsByShipAddress[ itr ]

                    for ( var index = 0; index < arrAutoShips.length; index++ )
                    {
                        var objAutoShip = arrAutoShips[ index ];

                        if ( index == 0 )
                        {
                            stSalesOrderRec.setValue( { fieldId: "custbody_vv_webstore_instructions", value: objAutoShip.strInstructions } );
                        }
                        log.debug( "objAutoShip.paymentToken", objAutoShip.paymentToken )

                        if ( objAutoShip.paymentToken )
                        {
                            stSalesOrderRec.setValue( { fieldId: _SO_PAYMENT_OPTION, value: objAutoShip.paymentToken } );
                        }

                        //set transaction date = AUTO SHIP DATE (A1)
                        var autoshipDate = objAutoShip.dtAutoTrandate
                        if ( autoshipDate )
                        {
                            log.debug( "dtAutoTrandate- autoshipDate", autoshipDate )

                            var newautoshipDate = moment( autoshipDate ).format( "M/D/YYYY" )
                            log.debug( "dtAutoTrandate- newautoshipDate", newautoshipDate )

                            stSalesOrderRec.setValue( { fieldId: _SO_DATE, value: new Date( autoshipDate ) } );
                        }

                        stSalesOrderRec.setSublistValue( { sublistId: "item", fieldId: _SO_ITEM, value: objAutoShip.strItem, line: index } )
                        stSalesOrderRec.setSublistValue( { sublistId: "item", fieldId: _SO_DISCOUNT_ITEM, value: objAutoShip.strDscntItem || null, line: index } ) /
                            stSalesOrderRec.setSublistValue( { sublistId: "item", fieldId: _SO_QUANTITY, value: objAutoShip.flQuantity, line: index } )
                        stSalesOrderRec.setSublistValue( { sublistId: "item", fieldId: _SO_RATE, value: objAutoShip.stRate, line: index } )
                        stSalesOrderRec.setSublistValue( { sublistId: "item", fieldId: _SO_FREQUENCY, value: objAutoShip.intFrequency, line: index } )
                        stSalesOrderRec.setSublistValue( { sublistId: "item", fieldId: _SO_INTERVAL, value: objAutoShip.strInterval, line: index } )

                        if ( objAutoShip.boolRXItem )
                        {
                            stSalesOrderRec.setSublistValue( { sublistId: "item", fieldId: _SO_PET, value: objAutoShip.strPet, line: index } )
                            stSalesOrderRec.setSublistValue( { sublistId: "item", fieldId: _SO_VET, value: objAutoShip.strVet, line: index } )
                            stSalesOrderRec.setSublistValue( { sublistId: "item", fieldId: _SO_VET_AUTH, value: objAutoShip.strVetAuth, line: index } )
                        }

                        stSalesOrderRec.setSublistValue( { sublistId: "item", fieldId: _SO_AUTOSHIP_ITEM, value: objAutoShip.boolIsAutoShip, line: index } )
                        stSalesOrderRec.setSublistValue( { sublistId: "item", fieldId: _SO_LINKED_AUTO_SHIP_LINE, value: objAutoShip.strAutoShipLineId, line: index } ) //added per task 176811172
                        //PG 10/27/2021 Task ID: #180052385 https://www.pivotaltracker.com/n/projects/2344928/stories/180052385/comments/227616719
                        //if the ship method is set for this sales order, then use that, else set to UPS Ground.
                        if ( stSalesOrderRec.getSublistValue( { sublistId: "item", fieldId: "custcol_vv_ship_method", line: index } ) )
                        {
                            stSalesOrderRec.setSublistValue( { sublistId: "item", fieldId: _SO_INITIAL_SHIP_METHOD, value: stSalesOrderRec.getSublistValue( { sublistId: "item", fieldId: "custcol_vv_ship_method", line: index } ), line: index } )
                        } else
                        {
                            stSalesOrderRec.setSublistValue( { sublistId: "item", fieldId: _SO_INITIAL_SHIP_METHOD, value: _UPS_GROUND, line: index } )
                        }

                        //means this doesn't came from an SO/this came from a future dated autoship
                        //use Sales order reference to determine whether it is a future dated autoship line
                        //if it is a future dated autoship line, we should set the initial autoship line on the SO so that it
                        //doesnt create another autoship line and the user event will update the line unique key + SO reference + next ship date
                        if ( objAutoShip.strSORef === "" || objAutoShip.strSORef === null )
                        {
                            stSalesOrderRec.setSublistValue( { sublistId: "item", fieldId: _SO_PRESCRIPTION, value: objAutoShip.strPrescriptionId, line: index } )
                            /** ADDED PG 3/30/2021 
                             * Include the prescription item id on the sales order line for task #177272483
                            */
                            if ( objAutoShip.strPrescriptionId != null && objAutoShip.strPrescriptionId != "" )
                            {
                                stSalesOrderRec.setSublistValue( { sublistId: "item", fieldId: _SO_PRESCRIPTION_ITEM, value: getPrescriptionItem( objAutoShip.strPrescriptionId, objAutoShip.strItem ), line: index } )
                            }
                            /** ADDED PG END */
                            stSalesOrderRec.setSublistValue( { sublistId: "item", fieldId: _SO_DATE_PRESC_LINKED, value: new Date(), line: index } ) //KKJS 02-15-21
                            stSalesOrderRec.setSublistValue( { sublistId: "item", fieldId: _SO_INITIAL_AUTOSHIP_LINE, value: objAutoShip.strAutoShipLineId, line: index } )
                        }

                        log.debug( "objAutoShipsByShipAddress", JSON.stringify( objAutoShip ) )

                    }
                    var stSalesOrderId = stSalesOrderRec.save( { ignoreMandatoryFields: true } );
                    log.debug( "SALES ORDER CREATED = " + stSalesOrderId )

                }

            } catch ( Err )
            {
                log.debug( "Error@createSalesOrderAndUpdateAutoShipLineRecord", Err )
                log.error( "Error@createSalesOrderAndUpdateAutoShipLineRecord", Err )
            }
        }


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

        function getPrescriptionItem ( strPrescriptionId, strItem )
        {
            try
            {
                var strPrescriptionItemId = "";

                var objSearch = search.create( {
                    type: _PRESCRIPTION_ITEM,
                    filters: [
                        [ _PRESCRIPTION.prId, "anyof", strPrescriptionId ],
                        "AND",
                        [ _PRESCRIPTION.itemName, "anyof", strItem ]
                    ],
                    columns: [
                        search.createColumn( { name: "internalid", label: "Internal ID" } )
                    ]
                } );

                //get only the first one
                objSearch.run().each( function ( res )
                {
                    strPrescriptionItemId = res.id
                    return false;
                } );

                return strPrescriptionItemId

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

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

Script 2: Creates future auto-ship by copying past auto-ship but the new set will have different shipping dates based on frequency set on the record.

/**
 *@NApiVersion 2.x
 *@NScriptType MapReduceScript
 * 
 * REVISION HISTORY
 * ************************************************************************************************
 * Revision 1.0 : - Create Second set of line records copying primary which has SO ref,
 *                              - Script is powered of by a saved search created in UI
 *                              - Set processed status on primary line records
 *                              - Establish connection b/w primary and secondary line records
 *                              - Runs at 2pm
 *                            ************************************************************************************************
 */
define( [ 'N/search', 'N/record', 'N/runtime', './moment.js' ], function ( search, record, runtime, moment )
{

    const _TYPE_AUTO_SHIP_LINE = 'customrecord_vv_autoship_record';
    const _SAVED_SEARCH_ID = 'customsearch_vv_2nd_genertn_autoship_2pm';

    /* CUSTOM RECORD FIELDS */
    const _CR_STATUS = "custrecord_vv_autoship_status"
    const _CR_ITEM = "custrecord_vv_autoship_item"
    const _CR_SO_REF = "custrecord_vv_autoship_soref"
    const _CR_NEXT_SHIP_DATE = "custrecord_vv_autoship_nextshipdate"
    const _CR_INTERVAL = "custrecord_vv_autoship_interval"
    const _CR_FREQUENCY = "custrecord_vv_autoship_freq"
    const _CR_IS_AUTOSHIP_ITEM = "custrecord_vv_is_autoship_item";
    const _CR_LINE_UNIQUE_KEY = "custrecord_vv_autoship_line_unique_key";
    const _CR_RX_ORDER_STATUS = "custrecord_vv_autoship_rxorderstatus"
    const _CR_PRESCRIPTION = "custrecord_vv_autoship_prescription"
    const _CR_AUTO_SHIP_DATE = "custrecord_vv_autoship_date"
    const _CR_SECOND_GENERATION = "custrecord_vv_autoship_second_generation"
    const _CR_CC_PYMT_TOKEN_LOOKUP = "custrecord_vv_autoship_selected_card"
    const _CR_CC_PYMT_CARD_TOKEN = "custrecord_vv_payment_card_token"
    const _CR_CC_CUSTOMER = "custrecord_vv_autoship_customer_src";
    /* CUSTOM RECORD FIELDS */

    /* LIST STATUS FIELD VALUE */
    const _LIST_INELIGIBLE = 5;
    const _LIST_PROCESSED = 4
    /* LIST STATUS FIELD VALUE */

    /* LIST VALUES */
    const _DAYS = "1"
    const _WEEKS = "2";
    const _MONTHS = "3";
    const _QUARTERS = "4";
    const _YEARS = "5";
    /* LIST VALUES */

    function getInputData ()
    {
        var searchId = search.load( { id: _SAVED_SEARCH_ID } );  // SEARCH NAME: Second Generation Auto-Ship Creation Search 2pm(**DO NOT DELETE - USED IN SCRIPT**)

        return searchId;
    }

    function reduce ( context )
    {

        var firstIterationLineRecID = context.key;
        log.debug( "firstIterationLineRecID", firstIterationLineRecID )

        try
        {
            if ( firstIterationLineRecID )
            {
                //LOAD AND GET VALUES FROM PRIMARY LINE RECORDS

                var autoShipRec = record.load( {
                    type: _TYPE_AUTO_SHIP_LINE,
                    id: firstIterationLineRecID
                } )

                var soRef = autoShipRec.getValue( { fieldId: _CR_SO_REF } )
                log.debug( "so Ref", soRef )

                if ( soRef )
                {
                    var dtSecondAutoshipDate = autoShipRec.getValue( { fieldId: _CR_NEXT_SHIP_DATE } )
                    var strInterval = autoShipRec.getValue( { fieldId: _CR_INTERVAL } )
                    var intFrequency = parseInt( autoShipRec.getValue( { fieldId: _CR_FREQUENCY } ) )
                    var boolIsAutoShip = autoShipRec.getValue( { fieldId: _CR_IS_AUTOSHIP_ITEM } )

                    var cardLookup = autoShipRec.getValue( { fieldId: _CR_CC_PYMT_TOKEN_LOOKUP } )

                    // -------- COPY ACTION : START -------- //
                    //Similar to make a copy action in UI

                    var copyRecord = record.copy( {
                        type: _TYPE_AUTO_SHIP_LINE,
                        id: firstIterationLineRecID,
                        isDynamic: true
                    } );

                    //update payment card token field if empty
                    var pcToken = autoShipRec.getValue( { fieldId: _CR_CC_PYMT_CARD_TOKEN } )

                    if ( pcToken === null || pcToken === "" )
                    {
                        var entityId = copyRecord.getValue( { fieldId: _CR_CC_CUSTOMER } )

                        if ( entityId && cardLookup )
                        {
                            var pcNewToken = findPaymentCardToken( cardLookup, entityId )

                            if ( pcNewToken )
                            {
                                copyRecord.setValue( { fieldId: _CR_CC_PYMT_CARD_TOKEN, value: pcNewToken } );
                            }
                        }

                    }

                    //autoship line should contain an interval, frequency, and should be autoship = T

                    if ( strInterval && intFrequency && boolIsAutoShip )
                    {
                        //Undergo Calculation for Next ship date and set the field on the seconday record

                        var dtSecondNextshipDate = calculateNextShipDate( dtSecondAutoshipDate, strInterval, intFrequency )
                        log.debug( "dtSecondNextshipDate", dtSecondNextshipDate )//N2

                        var secondNextShipDate = moment( dtSecondNextshipDate ).format( "MM/DD/YYYY" )
                        log.debug( "secondNextShipDate", secondNextShipDate )

                        if ( secondNextShipDate != null )
                        {
                            copyRecord.setValue( { fieldId: _CR_NEXT_SHIP_DATE, value: new Date( secondNextShipDate ) } );

                        } else
                        {
                            copyRecord.setValue( { fieldId: _CR_NEXT_SHIP_DATE, value: null } );
                        }
                    } else
                    {
                        copyRecord.setValue( { fieldId: _CR_NEXT_SHIP_DATE, value: null } );
                    }
                }

                //Set the second iteration line record's "Autoship date" same as Nextship date 
                copyRecord.setValue( { fieldId: _CR_AUTO_SHIP_DATE, value: new Date( dtSecondAutoshipDate ) } );

                //Nullify unncessary field values that are copied from original to copy line records
                copyRecord.setValue( { fieldId: _CR_SO_REF, value: null } );
                copyRecord.setValue( { fieldId: _CR_LINE_UNIQUE_KEY, value: null } );
                copyRecord.setValue( { fieldId: _CR_RX_ORDER_STATUS, value: null } );
                copyRecord.setValue( { fieldId: _CR_PRESCRIPTION, value: null } );

                //Stoes ID of created new copy of line record
                var submitCopyRecordId = copyRecord.save( {
                    enableSourcing: true,
                    ignoreMandatoryFields: true
                } );
                log.debug( "submitCopyRecordId", submitCopyRecordId )

                // ---COPY ACTION COMPLETED--- //


                //set primary autoship status to processed and link the generated seconday line record to primary (connection point)

                autoShipRec.setValue( { fieldId: _CR_STATUS, value: _LIST_PROCESSED } );

                if ( submitCopyRecordId )
                {
                    autoShipRec.setValue( { fieldId: _CR_SECOND_GENERATION, value: submitCopyRecordId } );
                }

                autoShipRec.save();
            }


        } catch ( Err )
        {
            log.debug( "Error @map on record save", Err )
            log.error( "Error @map on record save", Err )
        }

    }

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

    function calculateNextShipDate ( dtTrandate, strInterval, intFrequency )
    {
        try
        {
            switch ( strInterval )
            {
                case _DAYS:
                    return moment( dtTrandate ).add( intFrequency, "days" )

                case _WEEKS:
                    return moment( dtTrandate ).add( intFrequency, "weeks" )

                case _MONTHS:
                    return moment( dtTrandate ).add( intFrequency, "months" )

                case _QUARTERS:
                    return moment( dtTrandate ).add( intFrequency * 3, "months" )

                case _YEARS:
                    return moment( dtTrandate ).add( intFrequency, "years" )

                default: return null
            }
        } catch ( e )
        {
            log.debug( "Error@calculateNextShipDate", e )
            log.error( "Error@calculateNextShipDate", e )
        }
    }

    function findPaymentCardToken ( cardLookup, entityId )
    {
        try
        {
            var paymentinstrumentSearchObj = search.create( {
                type: "paymentinstrument",
                filters:
                    [
                        [ "formulatext: {mask}", "is", cardLookup ],
                        "AND",
                        [ "customer", "anyof", entityId ],
                        "AND",
                        [ "paymentinstrumenttype", "anyof", "3" ]
                    ],
                columns:
                    [
                        search.createColumn( {
                            name: "internalid",
                            summary: "MAX",
                            sort: search.Sort.ASC,
                            label: "Internal ID"
                        } )
                    ]
            } );
            var searchResultCount = paymentinstrumentSearchObj.runPaged().count;
            log.debug( "paymentinstrumentSearchObj result count", searchResultCount );

            var pctNewId;

            paymentinstrumentSearchObj.run().each( function ( result )
            {
                pctNewId = result.getValue( paymentinstrumentSearchObj.columns[ 0 ] );
                return true;
            } );

            return pctNewId;

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


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

Leave a comment

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