New scripter here, and I'm trying to perform an it...
# suitescript
t
New scripter here, and I'm trying to perform an item search in scripting so that I can compare the item reorder point, preferred stock, and location quantity on hand to set a classification field for an item. My attempt was to perform a search that retrieves these columns for a specific inventory location (we have multiple locations even though we only use one of them). I'm getting the error "TypeError Cannot read properties of undefined (reading 'getValue')". I've attempted looking in the help documentation and the records browser, but I'm not able to identify what aspect of the search are causing me to get an undefined search value. I'd welcome any feedback. The code is in the thread -->
define(['N/currentRecord', 'N/ui/dialog', 'N/log', 'N/search'], function(currentRecord, dialog, log, search) {
    /**      *@NApiVersion 2.0      *@NScriptType ClientScript      * created by Tim Pedersen Nov 1 2021      * Things needing work      * 1) I cannot get the search to perform correctly. I get this error in the logs - "TypeError Cannot read properties of undefined (reading 'getValue')"      */     function pageInit(context) {         var rec = currentRecord.get(); // create a variable/nickname for reference this record later on in the code         var itemId = rec.getValue({ fieldId: 'internalid' })         var searchResults = search.create({             type: search.Type.INVENTORY_ITEM,             filters: [                 ["type", "anyof", "InvtPart"],                 "AND", ["internalid", "anyof", itemId],                 "AND", ["inventorylocation", "anyof", "OIT Warehouse"]             ],             columns: [                 search.createColumn({ name: "itemid", label: "Name" }),                 search.createColumn({ name: "inventorylocation", label: "Inventory Location" }),                 search.createColumn({ name: "locationquantityonhand", label: "Location Quantity On Hand" }),                 search.createColumn({ name: "locationpreferredstock", label: "Location Preferred Stock" }),                 search.createColumn({ name: "locationreorderpoint", label: "Location Reorder Point" })             ]         });         var result = searchResults[0]         var qoh = result.getValue({ column: "locationquantityonhand" });         var prefStock = result.getValue({ column: "locationpreferredstock" });         var reorderPoint = result.getValue({ column: "locationreorderpoint" });         log.debug({             title: 'Item ID is  -  ',             details: itemId         })         log.debug({             title: 'QOH for Line 0 is -  ',             details: qoh         })         log.debug({             title: 'Pref Stock for line 0 is -  ',             details: prefStock         })         log.debug({             title: 'Reorder Point for Line 0 is  -  ',             details: reorderPoint         })         return true     }     return { // this tells NetSuite when to run each script that we created above.         pageInit: pageInit, //pageInit is an event that fires whenever the page is loaded     } });
b
search.create does not return an array of search.Results
t
Meaning that I should replace 'var searchResults = search.create? with var searchResults = search.Results?
b
meaning that you should go through the documentation for search.create that i linked
it doesnt return what you think it does
m
search.create just returns a search definition (a bit like a saved search). You then need to use Search.run() or Search.runPaged() to run the search, returning a ResultSet object, and a function on that like ResultSet.each() or ResultSet.getRange() to retrieve the individual rows of your search result. As battk indicated this is all in the documentation but it can be a bit strange to wrap your head around at first.
🙌 1
Side note, the "OIT Warehouse" value you're using as an inventorylocation filter isn't going to work, that needs to be the internal ID of that location rather than the name.
Also bear in mind that you're going to get an error with that internalid filter being empty if this is running on a new item.