`/**` `*@NApiVersion 2.x` `*@NScriptType ClientS...
# suitescript
r
/**
*@NApiVersion 2.x
*@NScriptType ClientScript
*/
define(["N/log", "N/search", "N/runtime"], function (log, search, runtime) {
function fieldChanged(context) {
var currentRecord = context.currentRecord;
var sublistName = context.sublistId;
var fieldName = context.fieldId;
var numLines = currentRecord.getLineCount({
sublistId: "item",
});
log.debug('fieldName', fieldName);
var itemSearchObj = search.create({
type: "item",
filters: [
["isinactive", "is", "F"],
"AND",
["averagecost", "isnotempty", ""],
],
columns: [
search.createColumn({
name: "itemid",
sort: search.Sort.ASC,
label: "Name",
}),
search.createColumn({ name: "averagecost", label: "Average Cost" }),
search.createColumn({ name: "internalid", label: "Internal ID" }),
],
});
log.debug('itemSearchObj', itemSearchObj.length);
var itemVerified = searchAll(itemSearchObj.run());
log.debug('itemVerified', itemVerified);
var i = 0;
while( i < itemVerified.length ) {
var internalId = itemVerified[i].getValue({
name: "internalid",
label: "Internal ID",
});
var averagecost = itemVerified[i].getValue({ name: "averagecost", label: "Average Cost" });
if (internalId != '') {
if (fieldName === "item" && sublistName === "item"){
currentRecord.setCurrentSublistValue({
sublistId: "item",
fieldId: "custcol_averagecostofitem",
value: averagecost,
})
}
}
i++;
}
function searchAll(resultset) {
var allResults = [];
var startIndex = 0;
var RANGECOUNT = 1000;
do {
var pagedResults = resultset.getRange({
start: parseInt(startIndex),
end: parseInt(startIndex + RANGECOUNT),
});
allResults = allResults.concat(pagedResults);
var pagedResultsCount =
pagedResults != null ? pagedResults.length : 0;
startIndex += pagedResultsCount;
var remainingUsage = runtime.getCurrentScript().getRemainingUsage();
} while (pagedResultsCount == RANGECOUNT);
var remainingUsage = runtime.getCurrentScript().getRemainingUsage();
return allResults;
}
}
return {
fieldChanged: fieldChanged,
};
});
📌 1
👀 1
s
I don't understand the need for this script at all. Just set the column up to source from the average cost field from the item record. If you insist on scripting this for some reason, there is no reason to search on every single item record on fieldChanged (just make a filter of the item you want, or better use search.lookupFields). Client scripts should be as slim and fast as possible. This script is wasting a ton of time every single time any field is changed at all.
d
Hey, see this channel pin for how to insert a Slack "Snippit" (code block) into your post 📌https://netsuiteprofessionals.slack.com/archives/C29HQS63G/p1623277256413400
Unfortunately you can't add a snippit by editing an existing post, but you can edit the code out and instead post a snippit as a reply here
r
@Sandii how?
s
Change the definition of the field to source from the item record
r
i wanted to try through to understand the API scripting
is there a better way?
s
If you want to script it then follow along the other suggestions I made
r
where?
in the group? have you made a suggestion for a question like this before? i can see
s
I don't understand what you are asking. This is a field changed script, the first thing you should check on a fieldChanged script is what field/sublist are being modified. If the field/sublist that you do not care about is being modified, then this script should do nothing. Assuming you've added criteria to make sure the correct field/sublist are being modified, then read the itemId from the line that you want to get this information from. Once you have the itemId, either search.lookupFields() (highly preferred for getting small information from a record) to get the information or add an additional filter to your item search to only get the information that one item.