Skip to Content
Menu
This question has been flagged
2 Replies
485 Views

Hello...

I'm trying to automate a selection value on a selection field based on date difference, for example:

I have a date field let's call it "Starting date" and a selection field with [A,B,C,D] values...

1st: I want to get date difference between Today's date and "Starting date" (preferably in years)

2nd: Automate the selection on the selection field:

if less than a year: value "A" on the selection field will be automatically selected.

if more than a year & less than 3 years: value "B" will be automatically selected.

& etc.... 

P.S: I'm trying to work my way on computation by adding the formula ( datetime.date.today() ) but it doesn't return any value...

Can someone explain how's the proper way to do it? thanks :)

Avatar
Discard

Hello,

The posted answer may help you to reslove the problem!

Best Answer

Hi,

To compute the selection field based on the starting date, click on the 'MORE' button in the sidebar to open the form of the computation section.

Then paste the following code into the 'Compute' field:


for record in self:

    today = datetime.datetime.now().date()

    start_date = record.x_studio_starting_date

    if not start_date:

        record['x_studio_is_computed_field'] = False

    else:

        # Calculate years difference

        years = (today - start_date).days / 365.25

        if years < 1:

            record['x_studio_is_computed_field'] = 'A'

        elif years < 3:

            record['x_studio_is_computed_field'] = 'B'

        elif years < 5:

            record['x_studio_is_computed_field'] = 'C'

        else:

            record['x_studio_is_computed_field'] = 'D'


Here x_studio_is_computed_field is the selection field and x_studio_starting_date is the start date field. Replace x_studio_is_computed_field and x_studio_starting_date with your actual field names if they differ. Add the start date field to the Dependencies section under Advanced Properties to ensure the computed field updates automatically when the date changes.

Hope it helps

Avatar
Discard
Best Answer

Hello,

The following code may help you,

Output:

Check any clarify your doubts.

Avatar
Discard