i have a suitescript that creates a csv using N/qu...
# suiteanalytics
p
i have a suitescript that creates a csv using N/query. All works well except the transaction type column. When the csv is created I get undefined for the rows. Is this not the correct statement for transaction type?
BUILTIN_RESULT.TYPE_STRING("TRANSACTION"."TYPE") AS "TYPE" /*{type#RAW}*/,
and my csv line value:
${result.TYPE}
I got the select statement from an Analytics dataset where the column does show the correct value.
m
I get the same value if I use
type
or
BUILTIN_RESULT.TYPE_STRING("TRANSACTION"."TYPE") AS "TYPE" /*{type#RAW}*/
which you provided, so maybe you can just reference the
type
column. For sales orders, this returns "SalesOrd" which may, or may not, be what you want. Depending on your needs, you can use
BUILTIN.DF(type)
which will return "Sales Order" or just
recordtype
which will result in "salesorder". The latter is useful if you need to work with these records in a script.
p
same result. i am thinking it is somewhere in my WHERE statements but I am copying verbatim from the Export as SuiteQL TEXT so not sure why it works in analytics but not in my script.
j
Can you post your entire SQL?
p
@jen Rather than show the entire code which is hundreds of lines I went ahead and just created the 1 select for type and filter for last period. same result. csv has those rows as undefined. /** * @NApiVersion 2.1 * @NScriptType ScheduledScript */ define(['N/query', 'N/file', 'N/log'], (query, file, log) => { const execute = (context) => { try { // SQL query const sql = ` SELECT BUILTIN.DF(type) AS "MYTYPE" FROM "TRANSACTION" WHERE "TRANSACTION".postingperiod IN BUILTIN.PERIOD('LP', 'START', 'NOT_LAST', 'BETWEEN'); `; // Run the query const pagedResults = query.runSuiteQLPaged({ query: sql, pageSize: 1000 }); if (!pagedResults || pagedResults.count === 0) { log.audit('No Results', 'No data was returned for the query.'); return; } // Collect data and prepare CSV content let csvContent = 'MYTYPE\n'; pagedResults.pageRanges.forEach((pageRange) => { const page = pagedResults.fetch({ index: pageRange.index }); page.data.forEach((result) => { csvContent += `${result.values[0]}\n`; }); }); // Create the CSV file const csvFile = file.create({ name: 'query_results.csv', fileType: file.Type.CSV, contents: csvContent, folder: 13247524 // Folder ID }); const fileId = csvFile.save(); log.audit('CSV File Created',
File ID: ${fileId}
); } catch (e) { log.error('Error Generating Report', e.message); } }; return { execute }; });