Openedge record scope using for each and first-of

76 Views Asked by At

I have a bit of a puzzling problem that I have never encountered before. Running in a production environment this seems to be fine but in a standalone procedure editor or mpro session it is failing so I would like to understand why..

FOR EACH employee NO-LOCK BREAK BY employee.company:
    IF FIRST-OF(employee.company) THEN 
    DO:
        FIND company WHERE company.company = employee.company NO-LOCK NO-ERROR.
    END.

    MESSAGE FIRST-OF(employee.company) SKIP
            AVAILABLE company
         VIEW-AS ALERT-BOX. 
END.

I was expecting that providing the company record exists, for each employee record of that company I would see that the company record is available, this is not the case, once we hit the second occurrence of the company it is no longer available.

I can get it to work using strong scoping as seen below but I would like to know whether or not there is some kind of start up parameter or something that I am missing as, as stated above this works fine in a production environment in its program. I should add that the code in the production environment lives in a procedure in a standard .p.

DO FOR employee, company:
    FOR EACH employee NO-LOCK BREAK BY employee.company:
        IF FIRST-OF(employee.company) THEN 
        DO:
            FIND company WHERE company.company = employee.company NO-LOCK NO-ERROR.
        END.

        MESSAGE FIRST-OF(employee.company) SKIP
                AVAILABLE company
            VIEW-AS ALERT-BOX. 
    END.
END.
2

There are 2 best solutions below

2
Tom Bascom On

Your production code likely has a reference to COMPANY somewhere outside the FOR EACH.

To test that idea, instead of the strong-scoping block add something like:

FIND COMPANY where COMPANY.something = ? NO-LOCK NO-ERROR.
2
nwahmaet On

There's also potentially something strange relating to the procedure buffer scope.

If you put the for each into an internal procedure, and run the internal procedure, things work as you expect them to. Run in the main block of a .p, and the (department, in my case) record goes out of scope on the next iteration.

Take a look at https://abldojo.services.progress.com/?shareId=657895e120add51d93a67a9d