Using the suitelet and xml file how to get the exp date values in the PDF file

using this suitlet we can get details of the exp date in the pdf file
/**
 * @NApiVersion 2.1
 * @NScriptType MapReduceScript
 */
define(['N/search', 'N/record', 'N/log', 'N/file'],
    function (search, record, log, file) {


        /**
         * The input stage of the Map/Reduce script
         *
         * @param {Object} context - Input context
         * @param {string} context.type - The stage type (e.g., input, map, reduce, summarize)
         * @param {number} context.executionNo - The current execution number
         * @returns {Array|Object|Search|RecordRef} - Array of key/value pairs
         */
        function getInputData(context) {
            try {
                // Define your customer search here
                let fileSearchObj  = search.create({
                    type: 'file',
                    filters:[
                        ["folder","anyof","17539"]
                        
                     ], // Add filters as needed
                    columns: [search.createColumn({
                        name: "name",
                        sort: search.Sort.ASC,
                        label: "Name"
                     }),
                     search.createColumn({name: "internalid", label: "Internal ID"})] // Add columns as needed
                });
                let inputData = [];
                log.debug('customerSearch', fileSearchObj);
                fileSearchObj .run().each(function (result) {
                    log.debug('customerSearch', result);
                    inputData.push({
                        fileName: result.getValue({
                            name: "name",
                            label: "Name"
                        }),
                        internalid: result.getValue({name: "internalid", label: "Internal ID"}),
                    });
                    return true;
                });
                log.debug('inputData', inputData);
                return inputData;
            } catch (error) {
                log.debug('error', error);
            }


        }


        /**
         * The map stage of the Map/Reduce script
         *
         * @param {Object} context - Map context
         * @param {number} context.key - The key for the input data
         * @param {number} context.value - The value for the input data
         * @param {string} context.type - The stage type (e.g., input, map, reduce, summarize)
         * @returns {void}
         */
        function map(context) {
            try {
                var customerID
                var customerinternalID
                let Values =JSON.parse(context.value);
                log.debug('Processing Customer ID', Values);


                // Retrieve attachments for the customer
                var oldFolderId = 17539;
              
                 customerID=parseInt(Values.fileName.split(' ')[0])
                // Check if the file is available in the old folder
                log.debug('Values.fileID', Values.internalid);
               // let isFileInOldFolder = isFileInFolder(Values.fileID, oldFolderId);
               // log.debug('Processed Customer ID', isFileInOldFolder);
                //if (isFileInOldFolder) {
                    // Check if the file with the same name already exists in the new folder
                    let newFolderId = 1154802 // Set the new folder ID
                    let isFileInNewFolder = isFileInFolderByName(Values.fileName, newFolderId);
                    if(customerID!=null && customerID!=undefined&& customerID!=''&& JSON.stringify(customerID)!='null'){
                         customerinternalID=GetCustomer(customerID)
                    }
                  
                   log.debug('customerID', customerID!=null && customerID!=undefined&& customerID!=''&& JSON.stringify(customerID)!='null'  );
                   log.debug('customerID', JSON.stringify(customerID) );
                    if (!isFileInNewFolder && customerID!=undefined && customerID!=NaN  && customerID!=null && JSON.stringify(customerID)!='null') {
                        let fileObj = file.copy({
                            id: parseInt(Values.internalid),
                            folder: newFolderId,
                        });
                       // log.debug('fileObj', fileObj );
                        // var detach = record.detach({
                        //     record: {
                        //         type: 'file',
                        //         id: parseInt(Values.fileID) 
                        //     },
                        //     from: {
                        //         type: record.Type.CUSTOMER,
                        //         id:Values.Id
                        //     }
                        // })
                        var attach= record.attach({
                            record: {
                                type: 'file',
                                id: parseInt(fileObj.id)
                            },
                            to: {
                                type: record.Type.CUSTOMER,
                                id:customerinternalID
                            }
                        });
                    //   log.debug('submitFieldsPromise', detach );
                      log.debug('submitFieldsPromise', attach );
                    } else {
                        log.debug('File already exists in the new folder', 'Attachment Name: ' );
                    }
                // } else {
                //     log.debug('File not in old folder', 'Attachment ID: ');
                // }
            } catch (e) {
                log.error('Error processing attachment', e.toString());
            }


            return true;




        }
        function GetCustomer(ID) {
            try {
                // Define your customer search here
                let customerSearch = search.create({
                    type: search.Type.CUSTOMER,
                    filters: [
                        ["entityid","haskeywords",ID]
                    ],
                    columns: [
                        search.createColumn({
                            name: "internalid",
                            sort: search.Sort.ASC,
                            label: "ID"
                         })
                    ]
                });
        
                let inputData = [];
                customerSearch.run().each(function (result) {
                    inputData=result.getValue({
                        name: "internalid",
                        sort: search.Sort.ASC,
                        label: "ID"
                    });
                    return true;
                });
                return inputData;
            } catch (error) {
                log.error('Error in getInputData', error.toString());
                return [];
            }
        }
        
        function isFileInFolderByName(fileName, folderId) {
            try {
                let fileSearch = search.create({
                    type: 'file',
                    filters: [
                        ['folder', 'anyof', folderId],
                        'AND',
                        ['name', 'is', fileName]
                    ]
                });


                return fileSearch.run().getRange({ start: 0, end: 1 }).length > 0;
            } catch (e) {
                log.error('Error checking if file with the same name exists', e.toString());
            }


            return false;
        }


        /**
         * The summarize stage of the Map/Reduce script
         *
         * @param {Object} summary - Summary data
         * @returns {string} - Summary message
         */
        function summarize(summary) {
           
            return 'Map/Reduce script completed successfully.';
        }


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


    }
);


Leave a comment

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