Search code examples
oracle-apexoracle-apex-5

How to prepopulate date field based on other date field?


I have 2 date fields in oracle apex, Date1 is mandatory needs to be input by user and Date2 needs to prepopulate as (Date1+2 years) then user clicks the save button to save the values in database. Then, if user clicks Date2 and change to something else, it should take that value and save.

Eg: 1.User inputs Date1 - 02/07/2019
2.Date2 should automatically populate as - 02/07/2021 but should not be saved in database.
3.User clicks save button to save data.
4.User wants to change Date2 to - 05/10/2019
5.User clicks save to save data.

I tried to set value using dynamic action for step2, but step4 is not working whenever I click save button, it's recalculating back to Date1+2 years.

Please help.


Solution

  • Suppose you have P1_ID (primary key), P1_DATE1 and P1_DATE2 on your form.

    1. Create a dynamic action to set value of P1_DATE2 on change of P1_DATE1. Use PL/SQL Expression as type and PL/SQL Expression ADD_MONTHS(TO_DATE(:P1_DATE1,'DD/MM/YYYY'),24);
    2. Set "Fire on initialization" to false so it won't recalculate P1_DATE2 when the screen loads for an existing record - that might be the cause of the behaviour you're seeing.
    3. It makes sense that you only want the dynamic action to fire for a new record so you could add a server-side condition in your dynamic action of "ITEM IS NULL" for item P1_ID.