I have a scripted search that is triggered by a bu...
# suitescript
p
I have a scripted search that is triggered by a button where I would like an alert to appear if no results are found but am not able to figure out where to put if/else statement. Script below. Any tips appreciated:
Copy code
var mySearch = search.create({
   type: "customrecord506",
   filters:
   [
      ["custrecord254","anyof",showName]
   ],
   columns:
   [
      search.createColumn({name: "custrecord255"}),
   ]
})
console.log(mySearch);
mySearch.run()
  .getRange(0,1000)
  .forEach(function (result) {
     var itemId = result.getValue({name: "custrecord255" });    
      console.log(itemId);
          if (result) {
      rec.selectNewLine({ sublistId: 'item'})
      .setCurrentSublistValue({  
        sublistId: 'item',
        fieldId: 'item',
        value: itemId,  
        forceSyncSourcing: true
      })
      .commitLine({"sublistId": "item"});    
  }
  else {
    alert('No Items Found');
  }
  });
}
c
Get the search results from mySearch.run() into its own var and check the property that tells you how many results there are. If 0, then alert.
s
^ Basically stop and store between the
.getRange()
and
.forEach()
p
awesome. thanks.
z
Copy code
var hasResult = false; // before mySearch.run()....
hasResult = true; // as first line inside forEach loop
if (!hasResult) {alert...} just after mySearch.run()... block

dummy solution but it will work :)