Skip to main content

Building a CSV File Download Function from a Page List

Consider a scenario where we want to download recently added table data from the screen into a CSV format for analysis. Today, we will explore how to accomplish this in Pega.

An existing out-of-the-box activity named "pxConvertResultsToCSV" is available in the @baseclass, and you can utilize it without the need to create it from the ground up. The following example illustrates a sample screen: when the button is clicked, the Page List data on the screen is downloaded as a CSV file to the end user's local machine.


1.To begin, establish a Page List property. In this instance, I defined a Data class, "MyCo-Data-Item," and subsequently generated a Page List named "ItemList" in the Work class, referencing the Item class.


2.Position a table that references the Page List, and configure it to be inline-editable, allowing the addition of records directly from the screen. Additionally, position a button in close proximity to the table.


3.Configure the button by adding an "Open URL in Window" action for the Click event and specify an out-of-the-box activity named "pxConvertResultsToCSV." Ensure that "Use Page" is checked. The minimum parameters to set include:

CSVPropertyTypes: The properties you wish to output, separated by commas. Avoid inserting spaces between them (as it may result in empty data).   

Note: It's not necessary to specify all properties, only the ones you want to include in the CSV. Additionally, you have the flexibility to change their order as desired.

PageListProperty: Specify the name of the Page List property to be read in the primary page. In this example, set it to "ItemList."


4.That concludes the setup. Test the functionality to ensure that a separate window pops up, and the CSV file is successfully downloaded to your local machine.



Tips:

1.Popup Window

If you prefer not to have a popup window, you can enter "_self" in the Window Name parameter. This allows you to download the file directly within the current browser window.


2. File Name

The default file name is "RecordsToCSV.csv." If you wish to modify it, enter the desired name in the FileName parameter.


* Additionally, if you want to append a timestamp to the file name for uniqueness, you can achieve this by setting the AppendTimeStampToFileName parameter to true.
   
   Example: "MyFile_20220926T155814.858 GMT.csv"

3. CSV Header Name

If you wish to alter the header name in the CSV file from the actual property name, you can do so using the CSVPropHeaders parameter. For instance, the original property names in this example are "ID," "Name," and "Price," but you can change them to other names such as "#," "Nombre," "precio."

How to manage the top page within a thread?

The previous example was centered around the case context and reading from an embedded page under pyWorkPage. If you intend to handle the top page without a case context, you must specify the PageListProperty from the top page. A sample implementation is presented below.

Initially, generate a Report Definition and designate it as the source for the table.


Within the Cosmos theme, the results page is loaded onto the DCSPA_UserPortal/ACPRIMARY_0 thread. Within this thread, automatically check the name of the top page generated by the system. In this instance, it is "pgRepPgSubSectionDataEntryBB.pxResults."


Input "pgRepPgSubSectionDataEntryBB.pxResults" into the PageListProperty parameter. Ensure it is enclosed in double quotes; otherwise, it may fail at runtime.


Now, proceed to test by displaying the Page List data constructed by the Report Definition, and then click the download button.



Note:

If you are not utilizing pagination, the process should function smoothly. However, in cases where pagination is implemented, this approach will only download the first set of elements on the page.

In the example above, pagination is enabled with 5 rows per page. Consequently, the downloaded CSV file will contain only those 5 rows, despite the actual table having more than 100 rows.

It's important to note that this issue arises specifically in the top page created by the Report Definition and does not occur if it is an embedded page in pyWorkPage. I will elaborate on how to circumvent this problem.

Solution for Downloading the Complete Record List When Using the Top Page:

Firstly, let's examine the existing configurations. Verify that the "Enable paging" checkbox is selected in the "Report Viewer" tab of the Report Definition. If no modifications have been made, it should be enabled, and the default page size should be set to 50.


Despite the default page size in the Report Definition being set to 50, this value is not utilized on the screen. The page size on the screen is configured within the Table settings. In this instance, I have configured it to be 5.


With these settings, the system shows 5 rows per page on the screen, not 50. When you download a CSV file, the file only contains the first page, which consists of 5 rows. This is not the intended outcome.


To obtain a complete list, customization is required. Reconfigure the button and create an activity to be called in the "Open URL in Window" action.

In this example, the activity is named "DownloadCSVFileFromPage."


Now, configure the new activity. In the first step, invoke Rule-Obj-Report-Definition.pxRetrieveReportData. You can use the same Report Definition. Specify a page for pyPageName; in this example, I named it "tempPage." In the second step, invoke pxConvertResultsToCSV and pass "tempPage.pxResults" for PageListProperty.



Now, you can download more than 5 rows, but the limit is set to 50, which might not be sufficient.

To remove the default limit of 50 for page size, uncheck the "Enable paging" option in the Report Definition. This modification will not impact the table page size on the screen.


Now you should be able to download the complete list from the database.

Happy Learning :)

Comments

Popular posts from this blog

Understanding the Lock Mechanism in Pega

Now, let's delve into the Pega lock mechanism. To begin, Pega employs both database-level locks, which are of shorter durations (usually in milliseconds), and Pega-level locks, which extend over more extended periods (determined by user operations, often a few minutes). While you have the option to disable Pega-level locks using the Optimistic locking strategy (discussed later), database-level locks cannot be turned off; they are an integral part of the infrastructure layer. Moving forward, I'll elaborate on Pega-level locks. 1. Fundamentals of Pega-Level Locks When an individual initiates an assignment, the system transitions the state to "Perform" mode and acquires a lock. This lock data is then added to the "PR_SYS_LOCKS" table in the database, where it is systematically managed. Throughout this phase, no other user can access the same assignment. Upon the user's submission or cancellation of the assignment, the state shifts to "Review" mode...

How to set up JFrog Artifactory as a Repository in Pega.

Before delving into the topic, let's gain some understanding of what a repository is........... What is a repository? A repository is a centralized storage location where data, files, documents, or other digital assets are stored and managed. It serves as a single source of truth for organizing, versioning, and accessing these assets.  In the context of software development, a repository typically refers to a version control system, where developers store and manage source code, configuration files, documentation, and other project-related files. Version control systems like Git, Subversion (SVN), and Mercurial are commonly used repositories in software development. In addition to version control systems, repositories can also refer to databases, file systems, content management systems (CMS), or any other structured storage system used to manage digital assets. Overall, a repository provides a structured and organized way to store and manage digital assets, facilitating collaborat...