Can we call the internal Suitelet using the basic ...
# suitescript
n
Can we call the internal Suitelet using the basic auth header?
b
no
n
Can we call the external Suitelet using the basic auth header?
b
that goes under yes, but it also goes that you can call an external suitelet without the basic auth header
😂 1
n
I was trying to export a saved search to CSV using external suitelet. But since it has many records, the script limit reaches. I was hoping to use the basic auth headers and then export the saved search using the standard CSV export by redirecting to the URL.
b
how many rows are there?
n
So many! around 23K
b
sounds off
you should be able to get 199k search rows using a paged search
or 100k using a result set
n
without script timing out?
b
ive yet to see a suitelet time out
n
then there must be something else causing the issue. I will re-check
What do you mean by "you should be able to get 199k search rows using a paged search or 100k using a result set"? What's the difference?
b
one uses 5 points to get 1000 results
one uses 10 points
result sets are also much slower than paged searches, so its basically always wrong ResultSet.getRange
n
Oh, the points. Thanks for this.
@battk I have no issues with getting the 23K results but when trying to compile them in CSV format, the suitelet time out. Should I use the tokens to redirect it to a saved search export url?
b
still dont really know how you plan on using a saved search url without a login
n
what do you suggest?
b
depends on how large the csv file is
n
23K lines.
and Suitelet timing out, I would say very large.
b
if its over 10mb, then you may be running out of memory
n
what are my options to achieve this?
b
if smaller, fair chance your code is inefficient
n
But I am not getting to the part where the CSV file is generated.
b
what does your code look like
n
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
define(['N/https', 'N/search', 'N/record', 'N/file', './ns_handler'], (https, search, record, file, nsHandler) => {
   /**
    * Defines the Suitelet script trigger point.
    * @param {Object} scriptContext
    * @param {ServerRequest} scriptContext.request - Incoming request
    * @param {ServerResponse} scriptContext.response - Suitelet response
    * @since 2015.2
    */
   function onRequest(scriptContext) {
      var request = scriptContext.request;
      var response = scriptContext.response;
      var params = request.parameters;
      //try {
         if (request.method === 'GET') {
            var customerId = params.customer_id;
            var reqAuthKey = params.authentication_key;
            var customerRec = record.load({type: record.Type.CUSTOMER, id: customerId});
            var entityId = customerRec.getValue({fieldId: 'nameorig'});
            var authKey = customerRec.getValue({fieldId: 'custentity_q3t_integration_key'});
            log.debug('auth key', authKey);
            log.debug('reqAuthKey', reqAuthKey);
            if (authKey != reqAuthKey) {
               log.error('Authentication key didn\'t match.');

               response.write({
                  output: 'Invalid Request'
               });
               return;
            }

var searchId = 'customsearch_se_pricefile_standard';

          
            var searchObj = search.load({id: searchId, type: search.Type.PRICING});
            searchObj.filters = searchObj.filters.concat([
               search.createFilter({name: 'customer', operator: 'anyof', values: [customerId]})
            ]);
            var searchResults = getResults(searchObj);
            var content = results2CSV(searchResults);
            response.write({output: content});

            /*var pricingFile = file.create({
             name: entityId + '_pricing_file.csv',
             fileType: file.Type.CSV,
             contents: content,
             encoding: file.Encoding.UTF8
             });

             response.writeFile({
             file: pricingFile
             //isInline: true
             });*/
         }
      //}
      /*catch (e) {
         log.error({title: 'Error', details: e.message || JSON.stringify(e) || 'No Meaningful message'});
         response.write({
            output: 'An error has occurred. Please contact the Administrator' +
                  'Error: ' + e.message
         });
      }*/
   }

   function getResults(searchObj) {
      var runSearch = searchObj.run();

      var results = [];
      var count = 0;
      var pageSize = 1000;
      var start = 0;

      do {
         var subresults = runSearch.getRange({
            start: start,
            end: start + pageSize
         });
         results = results.concat(subresults);
         count = subresults.length;
         start += pageSize;
      } while (count == pageSize);

      log.debug('Results fetched', results.length);

      return results;
   }

   function results2CSV(searchResults) {
      log.debug('results2CSV');
      var csv = [];
      var cols = searchResults[0].columns || [];

      var headerLine = [];
      for (var j = 0; j < cols.length; j++) {
         var col = cols[j];
         var key = col.label || col.name;
         headerLine.push(key);
      }

      var valueLines = [];
      for (var i = 0; i < searchResults.length; i++) {
         try {
            var lineValue = [];
            var searchResult = searchResults[i];

            for (var j = 0; j < cols.length; j++) {
               try {
                  var col = cols[j];
                  var val = searchResult.getText(col) || searchResult.getValue(col) || '';
                  lineValue.push(val);
               }
               catch (e) {
                  log.error('Error::Column loop', e);
               }
            }
            valueLines.push(lineValue.join(','));
         }
         catch (e) {
            log.error('For loop', e);
         }
      }

      csv.push(headerLine.join(','));
      csv.push(valueLines.join('\n'));
      log.debug('csv', csv);
      return csv.join('\n');
   }

   return {onRequest};

});
b
your search code is not good
you wrote an infinite loop
n
It ends after 23K
b
theoretically you can fix it, but you really want to be using a paged search
n
Paged search will save the script from timing out?
b
ive yet to see a suitelet time out, so i dont know
n
Yeah, but it is timing out.
SSS_TIME_LIMIT_EXCEEDED
b
just as i told you earlier, getRange is slower and uses more points
n
yes, but point is not the issue here anymore.
I can get all the 23K results.
It times out when running results2CSV method
b
find out how large your file should be
a quick estimation would be running for 1000 results
then taking the size of the generated file and multiply by 23
n
You are saying that time out issue can also be caused by the memory?
b
yes, scripts have a fixed amount of memory and your script gets slower the closer you get to it
n
okay, I will give this a shot.