The below is the requirement create a script wher...
# suitescript
v
The below is the requirement create a script where we can export a SQL statement(select * from accounts) into a csv file in the file cabinet? I dont know how to proceed this? could anyone please help me?
b
you will need to learn the N/query Module, N/file Module, and potentially the N/task Module
if you want to run this on a schedule, then you will need either a Map/Reduce Script Type or a Scheduled Script Type
if you also require a prettier interface, you will want a Suitelet Script Type
its not mandatory for writing csvs, but in general papaparse is a good library for working with csvs
v
ok thank you
this is my code var parsedResults = JSON.parse(context.values); log.debug('parsedresults',parsedResults); var csvData = Papa.unparse(parsedResults, { newline: '\n' }); log.debug('csvData',csvData); But it does not create the csv. what iamdoing wrong?
b
expect to share enough code to reproduce the problem
the code you shared doesnt generate csvs
but if you were trying to start logging one, that code may or may not work depending on whats in context.values
v
define(['N/search','N/record','N/task','N/query','SuiteScripts/lib/papaparse'], function (search,record,task,query,Papa) { function getInputData(){ log.debug('entering getinputdata') var sql = ` SELECT * FROM projecttask `; var results = query.runSuiteQL({ query : sql, params : [] }).asMappedResults(); var resultsLength = results.length; log.debug('resultsLength',resultsLength); return results; } function reduce(context){ try{ var parsedResults = JSON.parse(context.values); log.debug('parsedresults',parsedResults); var csvData = Papa.unparse(parsedResults, { newline: '\n' }); log.debug('csvData',csvData); }catch(e){log.debug('error',e);} } function summarize(summary){ } return{ getInputData : getInputData, reduce : reduce, summarize : summarize } } );
b
pay closer attention to what you are parsing
you arent passing the correct value to unparse
v
sorry i really dont get it .
even i tried papa.parse(context.values)
b
what value are you passing to Papa.unparse
v
parsedresults
b
thats the variable
but what value is in the variable
v
its an object
b
good enough for now
and what type of inputs can go into Papa.unparse
v
json objects
b
look harder at the papaparse documentation
v
i mean array of objects
b
better, now you get to ask, is an object the correct value to pass
v
no an array should be the one to pass
using for loop?
b
i mean you could, but i dont think it will add much value
v
i dont think any clue other than for loop
b
for loops are for iterating through an existing array
you want to create a new one
if you dont know how to create an array, you really need to go through a javascript tutorial first
its one of the basics
v
ok i got it
i get this error FileReaderSync is not definted
b
papaparse has options for working with files in a browser, which will not work in suitescript since it runs on the server
you need to change the options of whatever line is throwing the error