RPGLE #3
View and Update in RPGLE
In our previous articles related to RPGLE we have discussed about Environment setup and data insertion. Here we will be focusing on displaying the inserted data and updating them.
Everything that you see while navigation through a system fallen under the front-end of the system. Displaying information is what matter here the most.
Let’s explore the process of displaying information on an RPGLE screen.
Displaying information for a given field value
To display information on the screen, a logical file created with the relevant key is required. The relevant key is a key field which we can use to identify the data row uniquely. Here we can either user one field as a key field or a combination of fields as a key filed list.
Let’s see how we can write a subroutine to retrieve the data.
The following capture shows a subroutine named DISPLAYSUBROUTINE, which is a typical subroutine to retrieve data from a physical file.
First we have to point the cursor to the lowest level of the file. For that we can use SETLL of CHAIN functions.
Let’s see how we can read a file using CHAIN function.
CHAIN (U_ID) USER001Z2;CHAIN — The function name
(U_ID) — Column name — This should be the Key filed of the logical file
USER001Z2 — Logical File name
N.B. — Key field of the logical file should be exactly same as the column we are looking for. In this example, USER001Z2 file should be keyed in U_ID.
Get Jananie Perera’s stories in your inbox
Join Medium for free to get updates from this writer.
If the program can find the ID that we are looking for, then the values should be assigned to the variables declared in the screen.
IF %FOUND (USER001Z2);
U_ID = UID;
U_NAME = UNAME;
U_MOB = UMOB;
U_ROLE = UROLE;
ENDIF;This code segment can be further enhanced to handle the scenario of data not being available in the database also.
BEGSR DISPLAYSUBROUTINE;
CHAIN (U_ID) USR001Z2;
IF %FOUND (USR001Z2);
U_ID = UID;
U_NAME = UNAME;
U_MOB = UMOB;
U_ROLE = UROLE;
ELSE;
U_ID = 0;
U_NAME = '';
U_MOB = 0;
U_ROLE = '';
MSG = 'NO RECORDS FOUND';
ENDIF;
ENDSR;Let’s move on to an alternative way of displaying data in the screen.
Here the only change occurred is the CHAIN keyword. Instead of CHAIN keyword SETLL is used. Therefore, the same code segment can be altered as follows,
BEGSR DISPLAYSUBROUTINE_2;
SETLL (U_ID) USR001Z2;
READE (U_ID) USR001Z2;
IF %FOUND (USR001Z2);
U_ID = UID;
U_NAME = UNAME;
U_MOB = UMOB;
U_ROLE = UROLE;
ELSE;
U_ID = 0;
U_NAME = '';
U_MOB = 0;
U_ROLE = '';
MSG = 'NO RECORDS FOUND';
ENDIF;
ENDSR;That’s all about display subroutines. Let’s move to the update section.
Updating a Physical File
When updating a specific record in the physical file, we cannot use the exact same logical file that we have used to insert data. A new logical file with a new record format has to be defined. Related physical is connected to the logical file using PFILE function. The same physical file is referred to the logical file with this.
For instance, a logical file looks like as follows,
Let’s define a new subroutine for update operation.
BEGSR UPDATESUBROUTINE;
CHAIN (U_ID) USR001Z2;
IF %FOUND (USR001Z2);
UID = U_ID;
UNAME = U_NAME;
UMOB = U_MOB;
UROLE = U_ROLE;
UPDATE USR0012;
U_ID = 0;
U_NAME = '';
U_MOB = 0;
U_ROLE = '';
ENDIF;
ENDSR;In this subroutine we are looking for a User ID which is passed by the user in our Logical file; The U_ID in the code segment. When considering why we are using a logical file here is that for the faster execution of the function.
According to the code, if the program could find the relevant value in the database table, in this scenario in our physical file, physical fill be updated with the user entered values; with the values currently in the display screen. The update command will execute the database update and then the value of every filed is cleared.
U_ID = 0;
U_NAME = '';
U_MOB = 0;
U_ROLE = '';Now the screen is ready for any other operations or executions!
That’s the basic idea about display and update data. If you have more articles or topics you’d like to discuss in the future, feel free to reach out. I’m here to help with any questions or information you may need.
Happy reading, and see you on the next article!







