define(['N/currentRecord','N/search','N/runtime'], function (currentRecord,search,runtime) {
/**
* Function to get unit price of an item
*/
function getUnitPrice(itemId,priceLevelIs){
try{
var inventoryitemSearchObj = search.create({
type: "inventoryitem",
filters:
[
["type","anyof","InvtPart"],
"AND",
["pricing.pricelevel","anyof", priceLevelIs],
"AND",
["internalid","anyof", itemId]
],
columns:
[
search.createColumn({
name: "itemid",
sort: search.Sort.ASC,
label: "Name"
}),
search.createColumn({name: "displayname", label: "Display Name"}),
search.createColumn({
name: "pricelevel",
join: "pricing",
label: "Price Level"
}),
search.createColumn({
name: "unitprice",
join: "pricing",
label: "Unit Price"
})
]
});
var priceArray=[];
var priceLevelValue;
var searchResultCount = inventoryitemSearchObj.runPaged().count;
console.log("inventoryitemSearchObj result count",searchResultCount);
inventoryitemSearchObj.run().each(function(result){
priceLevelValue=result.getValue({
name: "unitprice",
join: "pricing",
label: "Unit Price"})
priceArray.push(priceLevelValue)
return true;
});
return priceArray
}catch (e) {
console.log("error@getUnitPrice",e)
return []
}
}
/**
* Function to be executed when field is changed.
*/
function fieldChanged(context) {
try {
if (context.fieldId == 'custrecord_jj_cust_name') {
console.log('context', context)
var records = context.currentRecord;
console.log('records', records)
var customerIs= records.getValue({
fieldId:'custrecord_jj_cust_name'
});
var priceLevel=search.lookupFields({
type: search.Type.CUSTOMER,
id: customerIs,
columns: ['pricelevel']
});
var priceLevelArray=priceLevel.pricelevel
var len=priceLevelArray.length
if(priceLevelArray.length>0){
var priceLevelIs=priceLevelArray[0].value;
console.log("price is", priceLevelIs);
var priceLevel_is= records.setValue({
fieldId:'custrecord_jj_cust_price_level',
value:priceLevelIs
});
console.log("price level_is", priceLevel_is)
}
else{
var priceLevel_is= records.setValue({
fieldId:'custrecord_jj_cust_price_level',
value:'1'
});
console.log("priceLevel_is",priceLevel_is)
}
}
if (context.fieldId == 'custrecordjj_item' && context.sublistId == 'recmachcustrecordjj_parent'){
var records = context.currentRecord;
var customer= records.getValue({
fieldId:'custrecord_jj_cust_name'
});
if(customer) {
records.selectLine({
sublistId: 'recmachcustrecordjj_parent',
line: context.line
});
var itemId = records.getCurrentSublistValue({
sublistId: 'recmachcustrecordjj_parent',
fieldId: 'custrecordjj_item'
});
records.getCurrentSublistValue({
sublistId: 'recmachcustrecordjj_parent',
fieldId: 'custrecord_jj_descr'
});
var priceLevelIs = records.getValue({
fieldId: 'custrecord_jj_cust_price_level'
});
console.log("PL",priceLevelIs)
var res = getUnitPrice(itemId, priceLevelIs)
var unitPrice = res[0];
records.setCurrentSublistValue({
sublistId: 'recmachcustrecordjj_parent',
fieldId: 'custrecord_jj_price_level',
value: unitPrice
});
}
else{
alert("Please enter the Customer Name")
}
}
if(context.fieldId == 'custrecord_jj_price_level'){
var records = context.currentRecord;
records.selectLine({
sublistId: 'recmachcustrecordjj_parent',
line: context.line
});
var unitPrice= records.getCurrentSublistValue({
sublistId: 'recmachcustrecordjj_parent',
fieldId: 'custrecord_jj_price_level',
});
if(unitPrice>35000){
alert("Could not calculate cost for the selected Realtime rate. The maximum declared amount was exceeded. Maximum value per package: 50000 USD.")
}
}
} catch (e) {
console.log('error @ fieldChanged', e)
}
}
/**
* Function to be executed on validation of line.
*/
function validateLine(context) {
try {
var custRec = context.currentRecord;
if (context.sublistId == 'recmachcustrecordjj_parent') {
var customer = custRec.getValue({
fieldId: 'custrecord_jj_cust_name'
})
if (customer) {
return true;
} else {
alert("Please enter the Customer Name before adding items");
return false;
}
}
return true;
}catch (e) {
console.log("error@validateLine",e)
}
}
/**
* Function to be executed when saving the record.
*/
function saveRecord(context) {
try{
var records = context.currentRecord;
var lineCount=records.getLineCount({
sublistId:'recmachcustrecordjj_parent'
});
console.log("line count",lineCount)
if(lineCount>0){
return true;
}
else{
alert("You must enter at least one line item for this transaction.")
}
}catch (e) {
console.log("error@saveRecord",e)
}
}
return {
saveRecord:saveRecord,
validateLine:validateLine,
fieldChanged: fieldChanged,
};
});