Map Reduce Script to map the least expiration date from inventory detail record to the item record cutsom fields.

We need to show the expiration date for each item on PLP Page. The value of the field will be from the inventory detail record. Since the Item Record and Inventory Record are two individual records, we can’t get it directly in SCA website. So, we have mapped the value of the expiration date from the inventory record to the item record.

To set the least expiration date on the item record created a map reduce script which execute on daily basis.

/**
		
 * @NApiVersion 2.1
		
 * @NScriptType MapReduceScript
		
 */
		


		
/************************************************************************************************
		
*  CLIENTNAME: RedBox
		
 * REDRX-360 Show expiration date on the item
		
 * **********************************************************************************************
		
 *
		
 * Author: Jobin & Jismi IT Services LLP
		
 *
		
 * Date Created : 11-July-2024
		
 *
		
 * Created By:  Jobin & Jismi IT Services LLP
		
 *
		
 * Description : Map Reduce Script to map the least expiration date from inventory detail record to the item record cutsom fields.
		
 * 
		
 *
		
 * REVISION HISTORY
		
 *
		
 *
		
 ***********************************************************************************************/
		


		
define(['N/email', 'N/record', 'N/search'],
		


		
    /**
		
 * @param{email} email
		
 * @param{record} record
		
 * @param{search} search
		
 */
		


		
    (email, record, search) => {
		


		
        const getInputData = (inputContext) => {
		
            try {
		


		
                var itemSearchObj = search.create({
		
                    type: "inventoryitem",
		
                    filters:
		
                        [
		
                            ["isonline", "is", "T"],
		
                            "AND",
		
                            ["isinactive", "is", "F"],
		
                            "AND",
		
                            ["type", "anyof", "InvtPart"],
		
                        ],
		
                    columns:
		
                        [
		
                            search.createColumn({
		
                                name: "internalid",
		
                                summary: "GROUP",
		
                                label: "Internal ID"
		
                            }),
		
                            search.createColumn({
		
                                name: "expirationdate",
		
                                join: "inventoryNumber",
		
                                summary: "MIN",
		
                                label: "Expiration Date"
		
                            })
		
                        ]
		
                });
		
                var searchResultCount = itemSearchObj.runPaged().count;
		
                if (searchResultCount > 0) {
		
                    return itemSearchObj;
		
                } else {
		
                    return [];
		
                }
		
            }
		
            catch (e) {
		
                log.error("error@getInput", e);
		
                return [];
		
            }
		
        }
		


		
        function convertDateFormat(dateString) {       
		
            const date = new Date(dateString);
		
            const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
		
            // Get the day, month, and year from the Date object
		
            const day = date.getDate();
		
            const month = monthNames[date.getMonth()];
		
            const year = date.getFullYear();
		
            // Format the date in 'dd-MMM-yyyy' format
		
            const formattedDate = `${day}-${month}-${year}`;
		
            return formattedDate;
		
        }
		


		
        const reduce = (reduceContext) => {
		
            try {
		
                let searchResult = JSON.parse(reduceContext.values);
		
                var expirationDate = searchResult.values["MIN(expirationdate.inventoryNumber)"];
		
                var itemId = searchResult.values["GROUP(internalid)"].value;
		
                if (!!expirationDate) {
		
                    var formattedDate = convertDateFormat(expirationDate);
		
                    record.submitFields({
		
                        type: 'lotnumberedinventoryitem',
		
                        id: itemId,
		
                        values: {
		
                            "custitem_expiry_date": new Date(formattedDate)
		
                        },
		
                        options: {
		
                            enableSourcing: false,
		
                            ignoreMandatoryFields: true
		
                        }
		
                    });
		
                } 
		
            }
		
            catch (e) {
		
                log.error("error@summarize", e)
		
            }
		
        }
		
        const summarize = (summaryContext) => {
		


		
        }
		


		
        return { getInputData, reduce, summarize }
		
    });

Leave a comment

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