Question
Mindtree
AU
Last activity: 3 Jun 2020 21:14 EDT
Convert pxResults to CSV and place in server path
Hi Team,
Need to convert Pagelist having pxResults to CSV and the CSV file should be available in server path.
Any approaches?
-
Likes (1)
MADDULURI VENKATA SAI KALYAN -
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Areteans
GB
Hi Chaitanya,
you can use an activity rule with connect file method to generate the csv string and store in a file in server location.
Steps would be as follows :
- loop for each record of pxresults
- inside the loop, generate a csv string using map structured rule.
- append it to the file using connect file method.
Another way would be to check the OOTB functionality provided for data types where we can export import the content of local storage records in csv format. There is an activity which has a java step which is capable to convert a pagelist of anyclass into a csv string. you can check that way too.
Edit : Name of the Activity : pxConvertResultsToCSV
Mindtree
AU
Hi Shuvadeep,
Thanks for your reply.
1st approach:
Loop through the results --- How can we give Header details for CSV using Map Structured rule?
2nd approach:
Used OOTB activity pxConvertResultsToCSV to get string(Contains comma separated value).If we pass that string with operation as "Write value from Clipboard Property" in Connect File, CSV is generating but values are getting set in single cell of CSV.
Areteans
GB
first approach:
- you can write the header column using connect-file and mapstructured rule to the csv
- inside the loop, construct csv string of each row and write to the file
- outside the loop close the file,
second approach,
the output should come row wise, can you print the csv string to logfile before storing it to a file.
I think the end line delimiter is not added like "\r\n" , which is causing such problem, but that should not happen ideally.
Optum Global Solutions
IN
Hi Chaitanya,
Did you achieve this with pxConvertResultsToCSV activity ?? How did you write it to server location through connect-file?
Updated: 3 Jun 2020 21:14 EDT
Virtusa
IN
This worked for me after with minimal customization of OOTB activities
Step1: Customize the below two OOTB activities to your ruleset with your class names
Use OOTB pxDownloadDataRecordsAsCSV (Consider the steps from 6-8) to pull the records (Pass RD and Class Name as params). pxConvertResultsToCSV
Step2: Set the file location path from DDS ex: local.exportPathbefore the last java step.
Modify the Java code(Write to csv file at location java step) to write the .csv file to the location (You should have the access to the network path)
char sep = PRFile.separatorChar; String status=""; //String exportPath= tools.getProperty("pxProcess.pyText").getStringValue(); DateTimeUtils dtu = ThreadContainer.get().getDateTimeUtils();
String fileNameParam = tools.getParamValue("FileName"); if(fileNameParam.equals("")){ fileNameParam = "RecordsToCSV"; } Boolean appendTimeStamp = tools.getParamAsBoolean(ImmutablePropertyInfo.TYPE_TRUEFALSE,"AppendTimeStampToFileName");
This worked for me after with minimal customization of OOTB activities
Step1: Customize the below two OOTB activities to your ruleset with your class names
Use OOTB pxDownloadDataRecordsAsCSV (Consider the steps from 6-8) to pull the records (Pass RD and Class Name as params). pxConvertResultsToCSV
Step2: Set the file location path from DDS ex: local.exportPathbefore the last java step.
Modify the Java code(Write to csv file at location java step) to write the .csv file to the location (You should have the access to the network path)
char sep = PRFile.separatorChar; String status=""; //String exportPath= tools.getProperty("pxProcess.pyText").getStringValue(); DateTimeUtils dtu = ThreadContainer.get().getDateTimeUtils();
String fileNameParam = tools.getParamValue("FileName"); if(fileNameParam.equals("")){ fileNameParam = "RecordsToCSV"; } Boolean appendTimeStamp = tools.getParamAsBoolean(ImmutablePropertyInfo.TYPE_TRUEFALSE,"AppendTimeStampToFileName");
FileName+=fileNameParam; if(appendTimeStamp){ FileName+="_"; FileName+=dtu.getCurrentTimeStamp(); } FileName+=".csv"; String strSQLfullPath = exportPath+ sep + FileName; PRFile f = new PRFile(strSQLfullPath); try{ // Create file PROutputStream stream = new PROutputStream(f); PRWriter out = new PRWriter(stream, null, false); // Bug with Excel reading a file starting with 'ID' as SYLK file. If CSV starts with ID, prepend an empty space. if(CSVString.startsWith("ID")){ CSVString=" "+CSVString; } out.write(CSVString); byte[] bytes = CSVString.getBytes(); status = ABCD_utilities.UploadFileToNetwork(bytes, exportPath, FileName, 0 ); oLog.infoForced(status); //Close the output stream out.close(); }catch (Exception e) { oLog.error("Error writing csv file: " + e.getMessage()); }
Step 3: Custom function ABCD_utilities.UploadFileToNetwork
Input Parameters:
Name --> Java Type -->Pega Type
fileContent-->byte[]
networkLocation-->String-->Text
fileName-->String-->Text
maxFileSizeInMB-->Int-->Number
Java Code:
String status = null; networkLocation = networkLocation + "\\" + fileName; File file = new File(networkLocation);
/* Validate Inputs */ if ((networkLocation == null || networkLocation.equals("")) || fileName == null|| fileName.equals("") || fileContent == null) { status = "FAIL:F1-Upload failed due to missing information"; return status; }
int fileSize = fileContent.length/1048576;
/* Validate File Size */ if (maxFileSizeInMB > 0 && maxFileSizeInMB < fileSize) { status = "FAIL:F2-File size (" + fileSize + " MB) exceeded Max File Size Limit of " + maxFileSizeInMB + "MB"; return status; } try { ByteArrayInputStream inByteStream = new ByteArrayInputStream(fileContent); InputStream inStream = new BufferedInputStream(inByteStream); OutputStream outStream = new BufferedOutputStream(new java.io.FileOutputStream(file));
byte outBuffer[] = new byte[4096]; int len; while (( len = inStream.read(outBuffer)) != -1) { outStream.write(outBuffer, 0, len); } inStream.close(); outStream.flush(); outStream.close(); status = "SUCCESS:File Uploaded"; } catch (Exception ex) { oLog.error("Error writing uploaded file to Macess", ex); status = "FAIL:F99-Upload failed due to the following exception : " + ex.getMessage(); }
return status;
-
Prashanth Mukkala Geetha sudea