Replace the existing images with the new images in the Item

Description:

Replace the existing images with the new images from the provided new link and will also do a one-time import update to set the default image to the item.

We have to add the images to the corresponding item record

  • if any new file is added to the item image folder in the file cabinet on that day.
  • If yes it will see if the keyword “default ” is added to the file name.
  • If yes we will pull the item name from that file name and set the default image to that item in the body item image field.
  • The image is added in the file cabinet under the item-related images, we have uploaded the zip file

Solution

/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
*/
/************************************************************************************************
TGUS - 260 Replace the existing images with the new images in the Item
*********************************************************************************************
*
* Author: Jobin & Jismi IT Services LLP
*
* Date Created : 27th-June-2022
*
* Description : Replace the existing images with the new images in the Item
*
* REVISION HISTORY
*
***********************************************************************************************/
define(['N/record', 'N/search'],
/**
* @param{record} record
* @param{search} search
*/
(record, search) => {
/**
* Defines the function that is executed at the beginning of the map/reduce process and generates the input data.
* @param {Object} inputContext
* @param {boolean} inputContext.isRestarted - Indicates whether the current invocation of this function is the first
* invocation (if true, the current invocation is not the first invocation and this function has been restarted)
* @param {Object} inputContext.ObjectRef - Object that references the input data
* @typedef {Object} ObjectRef
* @property {string|number} ObjectRef.id - Internal ID of the record instance that contains the input data
* @property {string} ObjectRef.type - Type of the record instance that contains the input data
* @returns {Array|Object|Search|ObjectRef|File|Query} The input data to use in the map/reduce process
* @since 2015.2
*/

const getInputData = (inputContext) => {

try {
var fileSearchObj = search.create({
type: "file",
filters:
[
["folder", "anyof", "632"],
"AND",
["name", "contains", "default"],
"AND",
["created", "onorafter", "yesterday"]


],
columns:
[

search.createColumn({name: "folder", label: "Folder"}),
search.createColumn({name: "url", label: "URL"}),
search.createColumn({name: "created", label: "Date Created"}),
search.createColumn({
name: "created",
sort: search.Sort.ASC,
label: "Date Created"
}),
search.createColumn({name: "modified", label: "Last Modified"}),
search.createColumn({name: "filetype", label: "Type"}),
search.createColumn({name: "internalid", label: "Internal ID"})

]
});

log.debug("document search")
var searchResultCount = fileSearchObj.runPaged().count;
if (searchResultCount > 0) {
return fileSearchObj

} else
return []

} catch (err) {
log.debug("error@GetInputData", err)
return []
}
}

/**
* This function is used to get item details
* @param {String} itemName - Specifies item number
* @returns {Result[]|*[]}
*/
function itemSearch(itemName) {
try {
var itemSearchObj = search.create({
type: "item",
filters:
[
["name", "is", itemName],
"AND",
["type", "anyof", "InvtPart"]
],
columns:
[
search.createColumn({
name: "itemid",
sort: search.Sort.ASC,
label: "ITEM NUMBER"
}),
search.createColumn({name: "internalid", label: "Internal ID"}),
search.createColumn({name: "type", label: "Type"})
]

});
var searchResultCount = itemSearchObj.runPaged().count;
let res
if (searchResultCount > 0) {
res = itemSearchObj.run().getRange({
start: 0,
end: 2
})
return res;
} else
return []

} catch (err) {
log.debug("error@FunctionItemSearch", err)
return []
}

}


/**
* Defines the function that is executed when the reduce entry point is triggered. This entry point is triggered
* automatically when the associated map stage is complete. This function is applied to each group in the provided context.
* @param {Object} reduceContext - Data collection containing the groups to process in the reduce stage. This parameter is
* provided automatically based on the results of the map stage.
* @param {Iterator} reduceContext.errors - Serialized errors that were thrown during previous attempts to execute the
* reduce function on the current group
* @param {number} reduceContext.executionNo - Number of times the reduce function has been executed on the current group
* @param {boolean} reduceContext.isRestarted - Indicates whether the current invocation of this function is the first
* invocation (if true, the current invocation is not the first invocation and this function has been restarted)
* @param {string} reduceContext.key - Key to be processed during the reduce stage
* @param {List<String>} reduceContext.values - All values associated with a unique key that was passed to the reduce stage
* for processing
* @since 2015.2
*/
const reduce = (reduceContext) => {

try {

let data = reduceContext.values.map(JSON.parse)
log.debug("Data", data);
if (!data)
return false
let details = data[0].values
let imageInternalId = data[0].id
let foldName = details.folder.text
if (!foldName)
return false
let folderName = foldName.split("_")
let itemName = folderName[0]
//log.debug("itemName**",itemName)
let itemID
let itemSearchData = itemSearch(itemName)
log.debug('itemSearchData', itemSearchData)
if (itemSearchData.length < 1)
return false
itemID = itemSearchData[0].id
log.debug("itemID", itemID)
let itemDetails = record.load({
type: record.Type.INVENTORY_ITEM,
id: itemID,
isDynamic: true
});

itemDetails.setValue({
fieldId: "custitem_atlas_item_image",
value: imageInternalId
});

let finalRes = itemDetails.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
log.debug("finalRes", finalRes)

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

}

return {getInputData, reduce}

});

Leave a comment

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