You'll need to write a custom activity that does everything in pxConvertResultsToCSV Steps 1 and 2. However, you will replace step 3 of the activity with steps that attach the document and link it to the work object
The download popup is the behaviour of step 3 of pxConvertResultsToCSV.
I don't have a working sample available, if not I could have shown you. What you have to understand is that your file is in memory at the end of step 1 of pxConvertResultsToCSV. Specifically it's in the local variable CSVString. Look at Step 2 to see if any specific manipulation is being done to this in memory file data to ensure you are attaching a valid file.
Once you know you have a valid file in memory, you need to set it to the property pyAttachStream of a clipboard page of class Data-WorkAttach-File (your attachment page) before saving it. Simply saving an attachment isn't enough as you need to create and save a link (clipboard page of class Link-Attachment) that links the work object and the attachment. The work object needs to be updated with this link information as well.
Use Work-.Attachfile as reference on creating all the pages required. Reference to "FileData" at the bottom of Step 6 is the equivalent of your "CSVString". FileData contains user uploaded file data in a hashmap. In your case there is no uploaded file only the generated CSV file. Just so there's no confusion, CSVString and FileData are equivalent in usage in this scenario, one contains file from an upload while the other contains a generated file.
You can't use Attachfile directly as it assumes that the file to be attached has been uploaded by the user. This is shown in step 6. Essentially you will be replacing the functionality in Step 6 with steps to set your own file data to the attachment page. But the rest of the steps are needed to complete the file attachment and linking to workobject (case).
I have commented step 3 of the activity and setting pyAttachStream property of the attachment page to "Local.CSVString" and then linking it to the case. Now I am able to attach the file to the case but the data gets corrupted. Any suggestions.....?
Pega expects all the data encoded to Base 64 format. There was no problem with my data. When I click on the link of the attachment in pega to download or view the file, Pega runs a Final activity called “Work-.DisplayAttachFile”. This activity calls a function “SendFile” to send the file data. The 3rd parameter of this function is “aIsBase64Encoded” (data in base64 encoded format) and this final activity is passing this parameter as “true”.
Now we can’t change the Final activity so before I pass “Local.SCVString” as pyAttachStream, I am encoding it with “@(Pega-RULES:Default).Base64Encode(Local.CSVString)” and setting the encoded value as pyAttachstream of my attachment page.