define(['N/record', 'N/search','N/config'],
/**
* @param{record} record
*/
(record, search, config) => {
function toGetUrl(url) {
try {
let configRecObj = config.load({
type: config.Type.COMPANY_INFORMATION
})
urlLink = configRecObj.getText({
fieldId:'appurl'
})
if (url) {
url = urlLink + url
}
return url;
}
catch (e) {
log.error('error@ toGetUrl', e)
}
}
/**
* defines the function to get the OpenBox details And slicing the records according to the page index from the restlet request body * @param {*} pageIndex
* @returns
*/
function openBoxSearchDetails(pageIndex) {
try {
let openBoxDetailsSearchObj = search.create({
type: "customrecord_jj_open_box_item_log1108",
filters: [
["isinactive", "is", "F"]
],
columns: [
search.createColumn({ name: "name", label: "Name" }),
search.createColumn({ name: "custrecord_jj_item_name_openboxlog1149", sort: search.Sort.ASC, label: "Item Name/Number" }),
search.createColumn({ name: "custrecord_jj_image_openboxlog1702", label: "Primary Image" }),
search.createColumn({ name: "custrecord_jj_simage_openboxlog1702", label: "Secondary Image" }),
search.createColumn({ name: "custrecord_jj_timage_openboxlog1702", label: "Tertiary Image" }),
search.createColumn({ name: "custrecord_jj_descrip_openboxlog1149", label: "Description" }),
search.createColumn({ name: "custrecord_jj_brand_openboxlog_1149", label: "Brand/Manufacturer" }),
search.createColumn({ name: "custrecord_jj_class_openboxlog1149", label: "Class" }),
search.createColumn({ name: "custrecord_jj_status_openboxlog1149", label: "Status" }),
search.createColumn({ name: "custrecord_jj_sales_cost_openboxlog1149", label: "Sales Cost" }),
search.createColumn({ name: "custrecord_jj_sales_price_openboxlog1149", label: "Sales Price" }),
search.createColumn({ name: "custrecordjj_item_sold_openboxlog1203", label: "Item Sold" }),
search.createColumn({name: "custrecord_jj_itm_curt_loc_oblog1449", label: "Item Current Location"}),
search.createColumn({name: "custrecord_jj_serial_no_openboxlog1149", label: "Serial Number"})
]
});
let searchResultCount = openBoxDetailsSearchObj.runPaged().count;
log.debug('searchResultCount', searchResultCount);
let resultArray = [];
let pagedData = openBoxDetailsSearchObj.runPaged({ pageSize: 10 });
let pagedopenBoxDetailsSearchObj = pagedData.fetch({ index: pageIndex });
pagedopenBoxDetailsSearchObj.data.forEach(function (result) {
let Name = result.getValue({ name: "name", label: "Name" });
let ItemName = result.getText({ name: "custrecord_jj_item_name_openboxlog1149", sort: search.Sort.ASC, label: "Item Name/Number" });
let PrimaryImage = toGetUrl(result.getText({ name: "custrecord_jj_image_openboxlog1702", label: "Primary Image" }));
let SecondaryImage = toGetUrl(result.getText({ name: "custrecord_jj_simage_openboxlog1702", label: "Secondary Image" }));
let TertiaryImage = toGetUrl(result.getText({ name: "custrecord_jj_timage_openboxlog1702", label: "Tertiary Image" }));
let Description = result.getValue({ name: "custrecord_jj_descrip_openboxlog1149", label: "Description" });
let Brand = result.getText({ name: "custrecord_jj_brand_openboxlog_1149", label: "Brand/Manufacturer" });
let Class = result.getText({ name: "custrecord_jj_class_openboxlog1149", label: "Class" });
let status = result.getText({ name: "custrecord_jj_status_openboxlog1149", label: "Status" })
let cost = result.getValue({ name: "custrecord_jj_sales_cost_openboxlog1149", label: "Sales Cost" });
let price = result.getValue({ name: "custrecord_jj_sales_price_openboxlog1149", label: "Sales Price" });
let ItemSold = result.getValue({ name: "custrecordjj_item_sold_openboxlog1203", label: "Item Sold" });
let Location = result.getText({ name: "custrecord_jj_itm_curt_loc_oblog1449", label: "Item Current Location"});
let serialnumber = result.getValue({ name: "custrecord_jj_serial_no_openboxlog1149", label: "Serial Number"})
resultArray.push({ Name, ItemName, PrimaryImage, SecondaryImage, TertiaryImage, Description, Brand, status, Class, cost, price, ItemSold, Location, serialnumber });
return true; // Continue iterating over search results
});
return resultArray;
} catch (e) {
log.error('Error in mySearch @ openBoxSearchDetails:', e);
return [];
}
}
/**
* Defines the function that is executed when a POST request is sent to a RESTlet.
* @param {string | Object} requestBody - The HTTP request body; request body is passed as a string when request
* Content-Type is 'text/plain' or parsed into an Object when request Content-Type is 'application/json' (in which case
* the body must be a valid JSON)
* @returns {string | Object} HTTP response body; returns a string when request Content-Type is 'text/plain'; returns an
* Object when request Content-Type is 'application/json' or 'application/xml'
* @since 2015.2
*/
const post = (requestBody) => {
// Function to perform the search and return the results as an array
try {
log.debug('request body', requestBody);
let pageIndex = parseInt(requestBody.pageIndex - 1, 10);
if (!requestBody) {
let message = {
status: "Error",
message: 'The request should be in the form of { "pageIndex": 0 }',
isSuccess: "false"
};
return message;
}
else if (!requestBody.hasOwnProperty('pageIndex')) {
return {
status: "Error",
message: "pageIndex key is missing or wrong syntax",
isSuccess: "false"
}
}
else if (isNaN(pageIndex) || pageIndex < 0) {
let message = {
status: "Error",
message: "The pageIndex should be a non-negative integer.",
isSuccess: "false"
};
return message;
}
let responseArray = openBoxSearchDetails(pageIndex);
log.debug("My Array", responseArray);
log.debug("Array Length", responseArray.length);
return responseArray;
} catch (e) {
log.error('Error in post @ post:', e);
return {
status: "Error",
message: "An error occurred while processing the request.",
isSuccess: "false"
};
}
};
return { post }
});
This restlet script is used to fetch the details of the open Box Log items based on the page index the open box details are divided into a page of 10 results