Jira Code: BLUEL-26
Deploy UserEvent Script on Customer Special Price Editing custom Record. The cost is updated based on the COST ESTIMATE TYPE. Do the UOM conversion if needed. For Kit Items, the item cost should be the sum of member items.
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
/*******************************************************************************
* BLUEL - 14 UE Update Custom Record.
* **************************************************************************
*
* Date: 02-05-2019
*
* Author: Jobin & Jismi IT Services LLP
*
*****************************************************************************
**/
define(['N/file', 'N/url', 'N/search', 'N/runtime', 'N/record', 'N/https', 'N/ui/serverWidget'],
function (file, url, search, runtime, record, https, serverWidget) {
var main = {
afterSubmit: function (scriptContext) {
var newRecId = scriptContext.newRecord.id;
var newRec = record.load({
type: "customrecord_bluel10_jj_customer_pricing",
id: newRecId
});
//if (scriptContext.type == 'create') {
var itemId = newRec.getValue({
fieldId: 'custrecord_bluel10_jj_item'
});
var fieldLookUp = search.lookupFields({
type: search.Type.ITEM,
id: itemId,
columns: ['type']
});
if (fieldLookUp.type[0].value == 'InvtPart') {
var item = record.load({
type: "inventoryitem",
id: itemId,
isDynamic: true,
});
var totalprice = main.getPrice(item)
log.debug("totalprice",totalprice);
} else if (fieldLookUp.type[0].value == 'Kit') {
var totalprice = 0;
var item = record.load({
type: "kititem",
id: itemId,
isDynamic: false,
});
var numLines = item.getLineCount({
sublistId: 'member'
});
for (var k = 0; k < numLines; k++) {
var memberitemID = item.getSublistValue({
sublistId: 'member',
fieldId: 'item',
line: k
});
var memberfieldLookUp = search.lookupFields({
type: search.Type.ITEM,
id: memberitemID,
columns: ['type']
});
if (memberfieldLookUp.type[0].value == 'InvtPart') {
var memberitem = record.load({
type: "inventoryitem",
id: memberitemID,
isDynamic: true,
});
var price = main.getPrice(memberitem)
totalprice = totalprice + price
} else if (memberfieldLookUp.type[0].value == 'Assembly') {
var totalpricesub = 0;
var memberitem = record.load({
type: "assemblyitem",
id: memberitemID,
isDynamic: true,
});
var numLinessub = memberitem.getLineCount({
sublistId: 'member'
});
for (var j = 0; j < numLinessub; j++) {
var memberitemID1 = memberitem.getSublistValue({
sublistId: 'member',
fieldId: 'item',
line: k
});
// log.debug("memberitemID", memberitemID)
var memberfieldLookUp1 = search.lookupFields({
type: search.Type.ITEM,
id: memberitemID1,
columns: ['type']
});
if (memberfieldLookUp1.type[0].value == 'InvtPart') {
var memberitemsub = record.load({
type: "inventoryitem",
id: memberitemID1,
isDynamic: true,
});
} else if (memberfieldLookUp1.type[0].value == 'Assembly') {
var memberitemsub = record.load({
type: "assemblyitem",
id: memberitemID1,
isDynamic: true,
});
} else if (memberfieldLookUp1.type[0].value == 'NonInvtPart') {
var memberitemsub = record.load({
type: "noninventoryitem",
id: memberitemID1,
isDynamic: true,
});
} else if (memberfieldLookUp1.type[0].value == 'Service') {
var memberitemsub = record.load({
type: "serviceitem",
id: memberitemID1,
isDynamic: true,
});
}
var pricesub = main.getPrice(memberitemsub)
totalpricesub = totalpricesub + pricesub
}
totalprice = totalpricesub
} else if (memberfieldLookUp.type[0].value == 'NonInvtPart') {
var memberitem = record.load({
type: "noninventoryitem",
id: memberitemID,
isDynamic: true,
});
var price = main.getPrice(memberitem)
totalprice = totalprice + price
} else if (memberfieldLookUp.type[0].value == 'Service') {
var memberitem = record.load({
type: "serviceitem",
id: memberitemID,
isDynamic: true,
});
var price = main.getPrice(memberitem)
totalprice = totalprice + price
}
}
}
if(totalprice){
newRec.setValue({
fieldId: "custrecord_bluel10_jj_cost",
value: totalprice
});
}else{
newRec.setValue({
fieldId: "custrecord_bluel10_jj_cost",
value: 0
});
}
// }
var commencement_date = newRec.getValue({
fieldId: 'custrecord_bluel10_jj_commencement_date'
});
var todate = new Date()
if (todate.getDate() + "/" + todate.getMonth() + "/" + todate.getFullYear() == commencement_date.getDate() + "/" + commencement_date.getMonth() + "/" + commencement_date.getFullYear()) {
var clientid = newRec.getValue({
fieldId: 'custrecord_bluel10_jj_client'
});
var data = newRec.getValue({
fieldId: 'custrecord_bluel10_jj_item'
});
var newPrice = newRec.getValue({
fieldId: 'custrecord_bluel10_jj_new_price'
});
var customer = record.load({
type: record.Type.CUSTOMER,
id: clientid,
isDynamic: false,
});
var numLines = customer.getLineCount({
sublistId: 'itempricing'
});
for (var k = 0; k < numLines; k++) {
var itemid = customer.getSublistValue({
sublistId: 'itempricing',
fieldId: 'item',
line: k
});
if (itemid == data) {
customer.setSublistValue({
sublistId: 'itempricing',
fieldId: 'price',
line: k,
value: newPrice
});
newRec.setValue({
fieldId: "custrecord_bluel10_jj_item_pricing",
value: newPrice
});
}
}
customer.save()
}
var lastmodified = newRec.getValue({
fieldId: 'lastmodified'
});
log.debug("lastmodified", lastmodified)
newRec.setValue({
fieldId: "custrecord_bluel10_jj_date_modified",
value: new Date(lastmodified)
});
var userdetails = runtime.getCurrentUser();
newRec.setValue({
fieldId: "custrecord_bluel10_jj_last_modified_by",
value: userdetails.id
});
newRec.save()
},
getPrice: function (item) {
var cost;
var costestimatetype = item.getValue({
fieldId: 'costestimatetype'
})
log.debug("costestimatetype", costestimatetype)
if (costestimatetype == 'ITEMDEFINED') {
var saleunit = item.getValue({
fieldId: 'saleunit'
});
var baseunit = item.getValue({
fieldId: 'baseunit'
});
var costestimate = item.getValue({
fieldId: 'costestimate'
});
if((saleunit)&&(baseunit)){
cost= main.salebaseunit(item,costestimate);
}else{
cost = item.getValue({
fieldId: 'costestimate'
});
}
} else if (costestimatetype == 'AVGCOST') {
var saleunit = item.getValue({
fieldId: 'saleunit'
});
var baseunit = item.getValue({
fieldId: 'baseunit'
});
var averagecost = item.getValue({
fieldId: 'averagecost'
});
if((saleunit)&&(baseunit)){
cost= main.salebaseunit(item,averagecost);
}else{
cost = item.getValue({
fieldId: 'averagecost'
});
}
} else if(costestimatetype == 'PREFVENDORRATE'){
var saleunit = item.getValue({
fieldId: 'saleunit'
});
var baseunit = item.getValue({
fieldId: 'baseunit'
});
var averagecost = item.getValue({
fieldId: 'averagecost'
});
if((saleunit)&&(baseunit)){
cost= main.salebaseunit(item,averagecost);
}else{
cost = item.getValue({
fieldId: 'averagecost'
});
}
} else if (costestimatetype == 'LASTPURCHPRICE') {
var purchadeunit = item.getValue({
fieldId: 'purchaseunit'
});
var saleunit = item.getValue({
fieldId: 'saleunit'
});
var lastpurchaseprice = item.getValue({
fieldId: 'lastpurchaseprice'
});
if((purchadeunit)&&(saleunit)){
cost = main.purchaseordercost(item,lastpurchaseprice);
}else{
cost = item.getValue({
fieldId: 'lastpurchaseprice'
});
}
} else if(costestimatetype == 'PURCHORDERRATE'){
var purorderrate;
var itemSearchObj = search.create({
type: "item",
filters:
[
["internalidnumber","equalto",item.id],
"AND",
["transaction.type","anyof","PurchOrd"]
],
columns:
[
search.createColumn({
name: "itemid",
summary: "GROUP",
label: "Name"
}),
search.createColumn({
name: "formulacurrency",
summary: "MAX",
formula: "MAX({transaction.rate}) KEEP(dense_rank last order by {transaction.trandate}, {transaction.datecreated})",
label: "Formula (Currency)"
})
]
});
var searchResultCount = itemSearchObj.runPaged().count;
log.debug("itemSearchObj result count",searchResultCount);
itemSearchObj.run().each(function(result){
purorderrate = result.getValue({
name: "formulacurrency",
summary: "MAX",
label: "Formula (Currency)"
});
log.debug("purorderrate",purorderrate);
});
if(searchResultCount == 1){
var purchadeunit = item.getValue({
fieldId: 'purchaseunit'
});
var saleunit = item.getValue({
fieldId: 'saleunit'
});
if((purchadeunit)&&(saleunit)){
cost = main.purchaseordercost(item,purorderrate);
}else{
cost = purorderrate;
}
}else{
cost = 0;
}
} else if(costestimatetype == 'PURCHPRICE'){
var purchadeunit = item.getValue({
fieldId: 'purchaseunit'
});
var saleunit = item.getValue({
fieldId: 'saleunit'
});
var purchaseprice = item.getValue({
fieldId: 'cost'
});
if((purchadeunit)&&(saleunit)){
cost = main.purchaseordercost(item,purchaseprice);
}else{
cost = item.getValue({
fieldId: 'cost'
});
}
}else {
cost = item.getValue({
fieldId: 'cost'
});
}
return cost;
},
salebaseunit: function (item,costestimate){
var salecost;
var saleunit = item.getValue({
fieldId: 'saleunit'
});
var baseunit = item.getValue({
fieldId: 'baseunit'
});
var base_unit = item.getText({
fieldId: 'baseunit'
});
if(saleunit == baseunit){
cost = costestimate;
}else{
var unitstype = item.getValue({
fieldId: 'unitstype'
});
var unitstypeSearchObj = search.create({
type: "unitstype",
filters:
[
["internalidnumber","equalto",unitstype],
"AND",
["unitname","is",base_unit]
],
columns:
[
search.createColumn({name: "conversionrate", label: "Rate"})
]
});
var searchResultCount = unitstypeSearchObj.runPaged().count;
log.debug("unitstypeSearchObj result count",searchResultCount);
unitstypeSearchObj.run().each(function(result){
salecost = result.getValue({
name: "conversionrate",
label: "Rate"
});
});
cost = costestimate*salecost;
}
return cost;
},
purchaseordercost: function (item,purorderrate){
var cost;
var purchasecost;
var salescost;
var pur_cost = item.getValue({
fieldId: 'lastpurchaseprice'
});
log.debug("pur_cost", pur_cost);
var pur_unit = item.getText({
fieldId: 'purchaseunit'
});
log.debug("pur_unit", pur_unit);
var purunit = item.getValue({
fieldId: 'purchaseunit'
});
log.debug("pur_unit", pur_unit);
var unittype = item.getValue({
fieldId: 'unitstype'
});
log.debug("unittype", unittype);
var unitstypeSearchObj = search.create({
type: "unitstype",
filters:
[
["internalidnumber","equalto",unittype],
"AND",
["unitname","is",pur_unit]
],
columns:
[
search.createColumn({name: "conversionrate", label: "Rate"})
]
});
var searchResultCount = unitstypeSearchObj.runPaged().count;
log.debug("unitstypeSearchObj result count",searchResultCount);
unitstypeSearchObj.run().each(function(result){
purchasecost = result.getValue({
name: "conversionrate",
label: "Rate"
});
});
var saleunit = item.getText({
fieldId: 'saleunit'
});
var unitstypeSearchObj = search.create({
type: "unitstype",
filters:
[
["internalidnumber","equalto",unittype],
"AND",
["unitname","is",saleunit]
],
columns:
[
search.createColumn({name: "conversionrate", label: "Rate"})
]
});
var searchResultCount1 = unitstypeSearchObj.runPaged().count;
log.debug("unitstypeSearchObj result count",searchResultCount);
unitstypeSearchObj.run().each(function(result){
salescost = result.getValue({
name: "conversionrate",
label: "Rate"
});
});
//log.debug("saleunit", saleunit);
var baseunit = item.getValue({
fieldId: 'baseunit'
});
//log.debug("baseunit", baseunit);
var unittype = item.getValue({
fieldId: 'unitstype'
});
//log.debug("unittype", unittype);
if((saleunit)&&(baseunit)){
cost = (purorderrate/purchasecost)*salescost;
log.debug("cost",cost);
}else{
cost = item.getValue({
fieldId: 'cost'
});
}
return cost;
}
};
for (var key in main) {
if (typeof main[key] === 'function') {
main[key] = trycatch(main[key], key);
}
}
function trycatch(myfunction, key) {
return function () {
try {
return myfunction.apply(this, arguments);
} catch (e) {
log.debug("e in " + key, e);
}
}
};
return main;
});