Hello everyone! For a RESTlet script, I am trying...
# suitescript
k
Hello everyone! For a RESTlet script, I am trying to get the sales order ID from it's number. Would the following code be a good general direction? Currently, it's returning an id of -1.
Copy code
var filters = [];

filters.push(search.createFilter({
        name: 'mainline',
        operator: <http://search.Operator.IS|search.Operator.IS>,
        values: true
      }));

      log.debug('filters', filters);

      filters.push(search.createFilter({
        name: 'tranid',
        operator: <http://search.Operator.IS|search.Operator.IS>,
        values: soName
      }));

      var searchPO = search.create({
        type: record.Type.SALES_ORDER,
        columns: ['name'],
        filters: filters
      });

      var soArray = [];
      searchPO.run().each(function (searchResult) {
        soArray.push(searchResult.id);
        return true;
      });

      if (soArray.length > 0) {
        return soArray[0];
      }
w
Try ‘T’ on your mainline filter values instead of Boolean
s
or use SuiteQL as it should be both faster and simpler
k
Using 'T' produces the same result.
w
Try building out the saved search in UI to see if you criteria are correct and optionally (recommend) then you might want to use the chrome extension for exporting saved search to SuiteScript (NetSuite: Search Export by David Smith @dcrsmith)
💯 2
d
Copy code
require(['N','N/ui/dialog'], function(N,dialog) {
for(var n in N){window[n] = N[n];};
try{

    let tranid="SO-001519";
    
let sql = `SELECT id from transaction where tranid='${tranid}'`;
var _result = query.runSuiteQL({
    query: sql,
    params: []
}).asMappedResults();
console.log(_result);
  
    } catch(e){console.error(e.message);}})
👍 1
k
After using SuiteQL, I get "Syntax error: missing ; before statement"
Copy code
// Test sales order
    let tranid = 'SO0000073';
    let sql = `SELECT id from transaction where tranid=${tranid}`;

    var result = query.runSuiteQL({
      query: sql,
      params: []
    }).asMappedResults();

    log.debug('result', result);
Also, do I pass something into the params key?
d
looks like you might be missing the single quotes around the tranid injection of the sql
Copy code
let sql = `SELECT id from transaction where tranid='${tranid}'`;
k
Weird... still getting the same error even after hardcoding the SO.
Copy code
let sql = `SELECT id from transaction where tranid='SO0000073'`;
d
On the sales order open your console and run this:
Copy code
require(['N','N/ui/dialog'], function(N,dialog) {
for(var n in N){window[n] = N[n];};
try{

// Test sales order
    let tranid = 'SO0000073';
    let sql = `SELECT id from transaction where tranid='${tranid}'`;

    var result = query.runSuiteQL({
      query: sql,
      params: []
    }).asMappedResults();
console.log(result);
  
    } catch(e){console.error(e.message);}})
1
k
That allowed me to pull the internal id
d
You need the single quotes around the ${tranid}
k
@dcrsmith, I used the solution you provided with single quotes and received an illegal character error. Also, I hardcoded the SO in single quotes and log.debug produced []. Here's the result after running the query in SuiteQL.
s
as a quick note - the code above would require SS2.1
💯 1
1
k
That helped! I'll continue to work on the solution. Thank you, everyone!
d
Thanks Shawn/Kenneth. I probably shouldn't assume that. Everything I do now is 2.1.
k
I'll keep these in mind for future scripts.