wes_w
12/08/2022, 10:18 PMmissing ; before statement
wes_w
12/08/2022, 10:18 PMwes_w
12/08/2022, 10:18 PM/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
define(['N/log'],
/**
*
*
* @param {log} log
*/
function(log) {
function getPriceLevels() {
var arrPriceLevels = [];
var pricelevelSearchObj = search.create({
type: 'pricelevel',
//filters: filter,
columns:
[
search.createColumn({name: "name"})
]
});
var searchResultCount = pricelevelSearchObj.runPaged().count;
log.debug("pricelevelSearchObj result count", searchResultCount);
pricelevelSearchObj.run().each(function (result) {
// .run().each has a limit of 4,000 results
if (result.id) {
var obj = {};
var id = result.id;
var name = result.getValue('name');
obj.name = name;
obj.id = id;
arrPriceLevels.push(obj);
}
return true;
});
return arrPriceLevels;
}
function getGsaPriceLevelIds() {
let GsaPriceLevelIds = [4,23,3,60]; //Dealer, eCompanyStore, Promotional, State of New Jersey
let priceLevels = getPriceLevels();
priceLevels.forEach(function (priceLevel) {
if (priceLevel.name.indexOf("GSA") === 0) {
//name starts with the letters GSA
GsaPriceLevelIds.push(parseInt(priceLevel.id));
}
});
return GsaPriceLevelIds;
}
/**
* Function definition to be triggered before record is loaded.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @param {string} scriptContext.type - Trigger type
* @Since 2015.2
*/
function beforeSubmit(scriptContext) {
var GSApriceLevelIds = getGsaPriceLevelIds();
var recItem = scriptContext.newRecord;
var lowestLastColumnCommercialPrice = '0';
var priceRowsCnt = recItem.getLineCount({
sublistId: 'price'
});
log.debug({
title: 'Price Rows Count',
details: priceRowsCnt
});
var pricecolumnsCnt = recItem.getMatrixHeaderCount({
sublistId: 'price',
fieldId: 'price'
});
log.debug({
title: 'Price Columns Count',
details: pricecolumnsCnt
});
for (var k=0; k < priceRowsCnt; k++) {
var pricelevelId = recItem.getSublistValue({
sublistId: 'price',
fieldId: 'pricelevel',
line: k
});
log.debug({
title: 'Price level ID at Line ' + k,
details: pricelevelId
});
if (!GSApriceLevelIds.contains(pricelevelId)) {
for (var l=0; l < pricecolumnsCnt; l++) {
var price = recItem.getMatrixSublistValue({
sublistId: 'price',
fieldId: 'price',
line: k,
column: l
});
log.debug({
title: 'Price at Price Level ID ' + pricelevelId + ' Line: ' + k + ' Col: ' + l,
details: price
});
if (lowestLastColumnCommercialPrice == 0 && price) {
lowestLastColumnCommercialPrice = price;
}
//nlapiLogExecution('DEBUG', 'Columns Count', pricecolumnsCnt);
//nlapiLogExecution("DEBUG", 'price in loop: ' + (k/1)+1 + " : " + l, price + ' ' + lowestLastColumnCommercialPrice + ' ' + typeof(lowestLastColumnCommercialPrice));
if (price && price/1 < lowestLastColumnCommercialPrice/1) {
log.debug({
title: 'Price is lower than ' + lowestLastColumnCommercialPrice,
details: price
});
lowestLastColumnCommercialPrice = price;
//nlapiLogExecution("DEBUG", 'price in loop: ' + k+1 + " : " + l, price + ' ' + lowestLastColumnCommercialPrice + ' ' + typeof(lowestLastColumnCommercialPrice));
}
}
}
}
if (lowestLastColumnCommercialPrice > 0) {
recItem.setValue({
fieldId: 'custitem_lowest_commercial_price',
value: lowestLastColumnCommercialPrice.toFixed(2)
});
log.audit({
title: recItem.getValue({fieldId:'itemid'}) + ' Updated',
details: 'Lowest Commercial Price: ' + lowestLastColumnCommercialPrice
});
}
}
/**
* Function definition to be triggered before record is loaded.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @param {string} scriptContext.type - Trigger type
* @Since 2015.2
function afterSubmit(scriptContext) {
}
*/
return {
//beforeLoad: beforeLoad,
beforeSubmit: beforeSubmit
//afterSubmit: afterSubmit
};
});
if (!Array.prototype.contains) {
Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] == obj) {
return true;
}
}
return false;
};
}
wes_w
12/08/2022, 10:19 PMDavid B
12/08/2022, 10:19 PMbattk
12/08/2022, 10:23 PM@NApiVersion 2.1
wes_w
12/08/2022, 10:24 PMwes_w
12/08/2022, 10:24 PMbattk
12/08/2022, 10:29 PMlet
is a feature only available in suitescript 2.1wes_w
12/08/2022, 10:29 PMvar
's inbattk
12/08/2022, 10:30 PMfunction getGsaPriceLevelIds() {
let GsaPriceLevelIds = [4, 23, 3, 60]; //Dealer, eCompanyStore, Promotional, State of New Jersey
let priceLevels = getPriceLevels();
priceLevels.forEach(function (priceLevel) {
if (priceLevel.name.indexOf("GSA") === 0) {
//name starts with the letters GSA
GsaPriceLevelIds.push(parseInt(priceLevel.id));
}
});
return GsaPriceLevelIds;
}