I need your help with a small SPESDH modification

Hello,

I added the small piece of code below in SPESDH. The purpose is to calculate a field on the lines when the delivery is saved and/or validated.
it only works if the delivery contains one single line.

If I add a second line, the fields are calculated correctly, and I can see the lines being updated thanks to the AFFZO.
However, once the loop is complete and the delivery is saved, only the first line's field is saved, and the second line's field returns to 0.

$MYCODE

Local Integer X
If !clalev([ZITM]): Local File ITMMASTER[ZITM] : Endif

For X = 0 to [M:SDH1]NBLIG - 1
    Read [F:ZITM]ITM0=[M:SDH1]ITMREF([L]X)
    If [M:SDH1]Z_MYFIELD([L]X) = 0 Then
        [M:SDH1]Z_MYFIELD([L]X) = [M:SDH1]QTY([L]X)/[F:ZITM]Z_PDS
        [F:SDD]Z_MYFIELD = [M:SDH1]Z_MYFIELD([L]X)
        affzo [M:SDH1]Z_MYFIELD([L]X)
    Endif
Next

Rewrite [F:SDD]

Close File [ZITM]

Return


Do you have any idea why the second line does not retain the value?

Thank you for your help.