How to create a record in a Data Type with Activity rule in Pega (PRPC) Platform Application

Coach Abhishek
7 min readFeb 10, 2023

--

Creating a record in a database table is part of the most commonly performed database operations known as CRUD (stands for Create, Read, Update and Delete). The ability to create a data record programmatically in order to store real-time data in database is one of the most common expectations from an application based on any technology. The real-time data can flow into your application from another system through APIs or can be collected from users of your application through different user forms or screens.

We can store real-time data in a Pega Platform application by programmatically creating a data record in a Data Type or Data Tables (found in lower versions of the product).

A data record can be programmatically created in a Data Type in three different ways — through an Activity rule, or through a Data Transform, or through a Savable Data Page.

The method that uses an Activity rule is traditional, and is not recommended as a best practice. However, the activity rules still play an important rule in some areas of Pega Platform, and the understanding or knowledge of the activity rules continues to be a must-to-have skill for a Pega Developer. In view of this fact, let’s look at the step-by-step approach to create a record in a Data Type with an Activity rule.

Pre-requisites

#1 Data Type

For the demo purpose, I’ve already created a Data Type named User in the organizational layer. MyOrg-Data-User is the class of the data type, meaning that any record created in the data type would be an object of this class. As shown below, I’ve added some fields, viz. User ID, First Name, Last Name, etc. to the data type, and have manually added two data records.

User Data Type with two existing records

If you’re interested in knowing more about Data Types, and how to manually create a data record therein, then you can go through the article given below.

#2 Activity rule

Additionally, I’ve created an Activity rule through which I’m going to create a new record in the above data type. I’ve applied this activity rule in a Work class and a ruleset of the implementation layer as shown below. You can choose a class and a ruleset for the activity as per your requirement.

Activity applied to the Work class

With the pre-requisites out of the way, let’s get to the meat of the matter. Let’s define the logic in the above activity to create a new data record in the data type.

Step# 1 — Create new clipboard page

Firstly, you need to create a new clipboard page (essentially a class object) of the Data Type class (MyOrg-Data-User) in memory using Page-New method as shown below. I’ve named the clipboard page as UserPage. You can choose any other suitable name as well.

Page-New method

Make sure to define the class of the page in the Pages & Classes tab of the activity rule as shown below.

Pages & Classes tab

Step# 2 — Set properties on the clipboard page

Secondly, you need to set values to the properties (basically the fields created in the data type) on the above clipboard page (essentially an object of MyOrg-Data-User class) using the Property-Set method as shown below.

Property-Set method

If you’re curious to learn more about how to set property values on a clipboard page using an Activity rule, you would find the below article helpful.

Step# 3 — Save the clipboard page into database

Thirdly, you need to save or persist the clipboard page (basically an object of the Data Type class i.e. MyOrg-Data-User) into the database. Obj-Save method is one of the ways of persisting an object into database through an activity. Make sure to refer the clipboard page as step page of the Obj-Save step as shown below.

Obj-Save method

Step# 4 — Commit changes to database

Next, you need to commit your changes to the database. There is a number of ways to perform database commit. For simplicity, let’s use Commit method as shown below.

Commit method

Step# 5 — Clean up memory

Lastly, you need to clear the memory of the unwanted clipboard page using Page-Remove method as shown below. It is similar to cleaning up the memory of the objects that are no longer needed.

Page-Remove method

Step# 6 — Exception Handling

Optionally, you can build the exception handling into your activity. There are two steps in the activity that could result in exception — Obj-Save step and Commit step. If you’re trying to save some invalid data into database, the Obj-Save step would throw an exception at runtime. If there’s some problem with the database, the both — Obj-Save and Commit steps would fail and generate exception.

We can handle the exceptions by applying a Jump condition to the activity steps that could potentially result in exception. The jump condition specifies the instructions for the system to follow in case of an exception, so that the code execution does not prematurely end midway.

First, let’s apply a Jump condition to the Obj-Save step as shown below. With this jump condition, I’m directing the system to continue to the next step if everything is good. However, I’m instructing it to jump to a later step labelled Exp (you can select any label of your choice), and not to prematurely end the execution if it meets an exception.

Jump condition on Obj-Save step

Afterward, I’ve added another step at the end with the label Exp at which I’m going to write a certain message to the logs in event of exception. Log-Message method can be used to log a message as shown below. I’m going to log the User ID (that uniquely identifies a record) of the record which saw the exception to facilitate debugging later on.

Log-Message method

A Jump condition, along the same lines, can also be defined on the Commit step as shown below.

Jump condition on Commit step

If either the Obj-Save or Commit step throws exception, the execution would jump to the Log-Message step. And, if the both steps — database save and commit are success, the execution would sequentially continue to the Page-Remove step as per the above defined Jump conditions. In order to avoid unnecessarily writing the exception message to the logs when the both — database save and commit are success, you need to apply a Jump condition to the Page-Remove step as well to direct the system to exit the activity, and skip the last Log-Message step as shown below.

Jump condition on Page-Remove step

This marks the completion of the logic that needs to be written in the activity rule.

Step# 7 — Run the activity

Save the activity rule, if not already done. Run it by clicking on the Actions button, and selecting the Run option from the menu as shown below.

Run the activity

A window as shown below would pop-up. Click on the Run button in the pop-up window.

Run the activity

If there are no errors, you would see a success message as shown below.

Success message

Step# 8 — Verification

In order to verify that a new record is created, go back to the Records tab in the Data Type, and refresh the data records by clicking on Actions -> Refresh as shown below.

Refresh the Data Type records

After the refresh, you would see the new record added in the Date Type as shown below.

New data record added

--

--