Compare each and every field of a file using RPGLE

565 pts.
Tags:
#AS400 #RPGLE #as400
I am developing an utility for logging the changes made to a PF. The file has 20 fields in it. Currently, i am writing logic like below - 

D File E DS                  EXTNAME(File1)
 *                                                      
D P_File E DS                  EXTNAME(File1) Prefix(P_)

C                      If                        Field1 <> P_Field1
C                      Eval                   BeforeImage = P_Field1
C                      Eval                   AfterImage = Field1
C                      Eval                   Comment = 'Field1 changed'
C                      Write
C                      EndIf

C                      If                        Field2 <> P_Field2
C                      Eval                   BeforeImage = P_Field2
C                      Eval                   AfterImage = Field2
C                      Eval                   Comment = 'Field2 changed'
C                      Write                 
C                      EndIf

Similarly, performing write for all 20 fields. The program works fine. But, what I would like to understand is if there is an efficient way to write this code? (By avoiding any repetitions)
0

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Discuss This Question: 4  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • ToddN2000
    There may be other ways. Without knowing how the fields are being loaded and how they can be changed makes it hard to provide an alternative. Another question I would ask is how man fields can change at one time? Are these coming in from a screen as separate fields? part of a subfile or a combination ? You could possibly load the before and after values to an array and loop through it 20 times using an index.
    135,295 pointsBadges:
    report
  • TheRealRaven
    The file records can be declared as two DSes. The DSes can then be compared to see if any changes at all happened in each record. No need to test fields if the whole record is unchanged.

    The position of changed bytes in a record can be used to determine which field has been changed. The List Fields (QUSLFLD) API can provide an array of field names and offsets/lengths.
    36,320 pointsBadges:
    report
  • sri8707
    Hi ToddN2000, basically all the 20 fields are shown in a screen. User is allowed to change any field of those 20. The intention is to capture audit of all the fields that have been changed. So, if user changes 10 out of 20 fields, I will be writing 10 records into the audit file. And the audit file will look like below -

    Field changed
    Before value
    After value
    Data/Time
    565 pointsBadges:
    report
  • sri8707
    Hi TheRealRaven, I am declaring the file records as 2 DS only. And this entire program will be executed only when there is a change in any of the fields. 
    To provide an example: Consider there are 20 fields in the file. Of 20, 10 fields have been changed. So, this program gets invoked. My intention is to write 10 records into an audit file in below format -

    Field changed
    Before value
    After value
    Date/Time

    So, basically I need to compare before and after image of all 20 records to build these 10 records into audit file. Any better way to do this other than using manual field by field comparison of 20 fields?
    565 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: