4 Ответы Последний ответ: 11.10.2018 21:56, автор: MattMcGeek

    How Do I Populate An APEX Form's Field Upon Save?

    Alex P 7939892

      hi, when inserting/updating a row in a form over a table in Apex 5.1,

      I want to populate a MODIFIED_BY varchar(40) and a MODIFIED_DTTM (timestamp) field with the user info.

      So, MODIFIED_BY = the user logged in to APEX

      MODIFIED_DTTM = current timestamp

       

      Also, ONLY if a new row (insert), I want to set CREATED_BY and CREATED_Date fields similarly.

       

      I have those fields set as display only on the form (as I don't want the user to be able to type whatever they want).

      But, if they don't save the page (i.e. commit), I don't want it to happen.

       

      How can I do this?

       

       

        • 1. Re: How Do I Populate An APEX Form's Field Upon Save?
          MattMcGeek

          My advise would be to use this in a trigger at the database level.

           

           

              if inserting then
                  :new.created := sysdate;
                  :new.created_by := nvl(sys_context('APEX$SESSION','APP_USER'),user);
              end if;
              :new.updated := sysdate;
              :new.updated_by := nvl(sys_context('APEX$SESSION','APP_USER'),user);

           

          After the record is created, you can simply display this data if so desired, but make it display only in APEX, not editable.  Allow the trigger to handle that for you.

          • 2. Re: How Do I Populate An APEX Form's Field Upon Save?
            NelsonMendes

            Hi Alex,

             

            You should have two buttons, Create and Save.

            When you push the Create button it means the record is new otherwise is a modification.

             

            Just create a page processes that fires upon each event Create and Save and update the fields.

            To control the buttons you can use the primary key column, if null the its a new record otherwise...

            • 3. Re: How Do I Populate An APEX Form's Field Upon Save?
              Alex P 7939892

              hi Thanks Matt!

               

              For anyone else, I instantiated Matt's idea in full this way (and tested successfully):

               

              CREATE OR REPLACE TRIGGER  "CYCLE_STATUS_DDL_TRIG"
              BEFORE INSERT OR UPDATE
              ON CYCLE_STATUS
              FOR EACH ROW
              BEGIN
              if inserting then
                      :new.created_date := sysdate;
                      :new.created_by := nvl(sys_context('APEX$SESSION','APP_USER'),user);
              end if;
              --at each mod, update the mod fields
                  :new.modified_dttm := sysdate;
                  :new.modified_by := nvl(sys_context('APEX$SESSION','APP_USER'),user);
              END;

              /
              ALTER TRIGGER  "CYCLE_STATUS_DDL_TRIG" ENABLE
              /

              • 4. Re: How Do I Populate An APEX Form's Field Upon Save?
                MattMcGeek

                Alex P 7939892,

                Good deal.  Yes, it's much easier to let the DB handle such activities.  And any other product outside of APEX were to change a record, it handles that as well which is nice.  A catch all function.