Hiya,
I'm working on trying to improve the time it takes to process a User Event script on my Sales Orders that afterSubmit will look up the cost values of items on the transaction and save the record with Gross Profit and Gross Profit Percentage values for each item line.
I think for start I can save a little time by swapping this from afterSubmit to beforeSubmit, right?
But the main area I think I can save time on is if I can remove the need to search for the cost values from the item records.
At the moment I have these searches in my script:
function getItemCost(itemId, locationId) {
try {
var itemData = search.lookupFields({
type: search.Type.ITEM,
id: itemId,
columns: [
'costestimate',
'averagecost',
'lastpurchaseprice',
'cost'
]
});
And incase none of those field return a value I then run:
function getLocationAverageCost(itemId, locationId) {
try {
var result = search.create({
type: 'item',
filters: [
['internalid', 'anyof', itemId],
'AND',
['inventorylocation.internalid', 'anyof', locationId]
],
columns: [
search.createColumn({
name: 'locationaveragecost'
// join: 'inventoryLocation'
})
]
}).run().getRange({ start: 0, end: 1 });
I'm sure I can cut these searches out if I could attach the item cost to the item line information, then I could just get these values directly from the Sales Order record instead of needing to search the Item Record.
Thing is, management doesn't want our sales team to see the cost prices of the items, just the GP and the GP% and I'm pretty sure if I added these cost columns as hidden that a User Event script isn't able to access hidden fields/columns, is that right?
Is there some other way I can reduce the time my script is taking likely due to the searches?
Perhaps I could try using N/query module instead of the search module? Or is N/record .lookupFields() a faster option than query?
I can probably, if any of these are the correct option, improve it even further by looking up the cost of all items at once rather than as I loop through them.