Requirement
The client needs to implement the promotions feature in NetSuite. Therefore, need to transfer the promotion code record to the website. Map reduce script is created to transfer promotion codes to the Website. When triggered will transfer the promotion codes to the website.
Solution
/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
*/
define(['N/record', 'N/search', 'N/https', 'N/runtime'],
/**
* @param{record} record
* @param{search} search
* @param{https} https
* @param{runtime} runtime
*/
(record, search, https, runtime) => {
const getInputData = (inputContext) => {
try {
let promotionCodes = getPromotionCodes()
log.debug('promotionCodes', promotionCodes)
return promotionCodes
} catch (e) {
log.error('error@getInputData', e)
}
}
const map = (mapContext) => {
}
const reduce = (reduceContext) => {
try {
log.debug('In reduce')
let bodyObj = {}
let promotionCodes = JSON.parse(reduceContext.values);
log.debug('promotionCodes in reduce',promotionCodes)
let promotionRecord = record.load({
type: record.Type.PROMOTION_CODE,
id: promotionCodes.id,
isDynamic: true
})
let getPromoValues = getPromotionRecordValues(promotionRecord)
let discountItemValues=getPromotionRecordDiscountValues(promotionRecord)
let getBodyObjValues = setPromoRecordValues(promotionCodes, promotionRecord, getPromoValues, bodyObj,discountItemValues)
let environmentCheck = JSON.stringify(runtime.envType);
if (runtime.EnvType.SANDBOX) {
log.debug('You are using your sandbox!');
let response=apiCalling(getBodyObjValues)
log.debug("response",response);
// log.debug("response body",response.body)
let body=JSON.parse(response.body)
log.debug("Body......",body)
} else if (runtime.EnvType.PRODUCTION) {
log.debug('You are using your production!');
}
} catch (e) {
log.error('error@reduce', e)
}
}
const summarize = (summaryContext) => {
}
function getPromotionCodes() {
try {
let promotionCodeSearchObj = search.create({
type: "promotioncode",
filters:
[
["internalid","anyof","7"]
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"}),
search.createColumn({
name: "name",
sort: search.Sort.ASC,
label: "Name"
}),
search.createColumn({name: "code", label: "Coupon Code"}),
search.createColumn({name: "isinactive", label: "Inactive"}),
search.createColumn({name: "startdate", label: "Start Date"}),
search.createColumn({name: "enddate", label: "End Date"}),
search.createColumn({
name: "canbeautoapplied",
label: "This Promotion Can Be Automatically Applied"
}),
search.createColumn({name: "description", label: "Description"}),
search.createColumn({name: "discount", label: "Discount Item For Accounting"}),
search.createColumn({name: "discountrate", label: "Discount Rate"}),
search.createColumn({name: "combinationtype", label: "Combination With Other Promotions"}),
search.createColumn({name: "fixedprice", label: "Fixed Price"}),
search.createColumn({name: "itemquantifier", label: "Item Quantity"}),
search.createColumn({name: "isinactive", label: "Inactive"})
]
});
let promotionCodesArray = []
let searchResults = promotionCodeSearchObj.run().getRange({
start: 0,
end: 1000
})
if (searchResults.length > 0) {
for (let i = 0; i < searchResults.length; i++) {
promotionCodesArray.push(searchResults[i])
}
return promotionCodesArray
} else {
return []
}
} catch (e) {
log.error('error@getPromotionCodes', e)
}
}
function getPromotionRecordValues(rec) {
try {
let itemDetail = []
let itemListCount = rec.getLineCount({
sublistId: 'items'
})
// log.debug('lineCount',itemListCount)
if (itemListCount > 0) {
let itemDet = {}
for (let i = 0; i < itemListCount; i++) {
itemDet.itemId = rec.getSublistValue({
sublistId: 'items',
fieldId: 'item',
line: i
})
itemDet.itemName = rec.getSublistValue({
sublistId: 'items',
fieldId: 'item_display',
line: i
})
itemDetail.push(itemDet)
}
// log.debug('itemDet',itemDet)
return itemDetail
} else {
return []
}
} catch (e) {
log.error('error@getPromotionRecordValues', e)
}
}
function getPromotionRecordDiscountValues(rec) {
try {
let disItemDetail = []
let discountItemListCount = rec.getLineCount({
sublistId: 'discounteditems'
})
// log.debug('lineCount',itemListCount)
if (discountItemListCount > 0) {
let disItemDet = {}
for (let i = 0; i < discountItemListCount; i++) {
disItemDet.itemId = rec.getSublistValue({
sublistId: 'discounteditems',
fieldId: 'discounteditem',
line: i
})
disItemDet.itemName = rec.getSublistValue({
sublistId: 'discounteditems',
fieldId: 'discounteditem_display',
line: i
})
disItemDetail.push(disItemDet)
}
// log.debug('itemDet',itemDet)
return disItemDetail
} else {
return []
}
} catch (e) {
log.error('error@getPromotionRecordValues', e)
}
}
function setPromoRecordValues(promotionCodes, promotionRecord, getPromoValues, bodyObj,discountItemValues) {
try {
bodyObj.promotionId=promotionCodes.values.internalid[0].value
bodyObj.name = promotionCodes.values.name;
bodyObj.description = promotionCodes.values.description;
bodyObj.isInactive=promotionCodes.values.isinactive;
bodyObj.customform={
formId:promotionRecord.getValue({
fieldId:'customform'
}),
formName:promotionRecord.getText({
fieldId:'customform'
})
}
bodyObj.startdate = promotionCodes.values.startdate;
bodyObj.enddate = promotionCodes.values.enddate;
bodyObj.combinationtype = promotionCodes.values.combinationtype;
bodyObj.canbeautoapplied = promotionCodes.values.canbeautoapplied;
bodyObj.discount = promotionCodes.values.discount;
bodyObj.code = promotionCodes.values.code;
bodyObj.items = getPromoValues;
bodyObj.discountrate = promotionCodes.values.discountrate;
bodyObj.discountitemtype=promotionCodes.values.discountitemtype;
bodyObj.discounteditems=discountItemValues;
bodyObj.fixedprice = promotionCodes.values.fixedprice;
bodyObj.itemquantifier = promotionCodes.values.itemquantifier;
// bodyObj.name=promotionCodes.values.nam;
return bodyObj
} catch (e) {
log.error('error@setPromoRecordValues', e)
}
}
function apiCalling(bodyObj) {
let headerObj = {
"api-key": "####",
"Content-Type": "application/json",
"Authorization": "Bearer #######"
}
let response = https.post({
url: 'https://########',
body: JSON.stringify(bodyObj),
headers: headerObj
});
return response
}
return {getInputData, reduce, summarize}
});