0

Automatic Calculating an end date based on new records

I'm quite new in Ninox and have a question on how to let Ninox calculate an end-date based on newly added records in a table

I have created a trigger whenever I change the status of a cow to add a record in a table which need to keep track of the status of the cow (Pregnant, Sick, etc).

I have now 3 columns: Date, Name Cow and New Status. All are takes from the action of changing the status of the cow in the Cow table.

Is there a way of adding a formula field as 'End Date' which can find the first earlier date of the same cow in that table and take this date as End date?

For example:

Date                  Name Cow      New Status          End Date

07-01-2024      Alisha               Pregnant              ?

07-22-2024      Alisha               Normal                  ?

 

I want to let the End date column to be populated now by '07-22-2024'.

If it cannot find a next date it can show 'N/A' or something and if it can find a next available date, I needs to show this date. 

Is there a way to accomplish this? Any help would be welcome.

2 replies

null
    • szormpas
    • 4 mths ago
    • Reported - view

     Hi,

    You can achive this directly from the trigger by modifing your code as below:

    let i := this;
    let previousStatus := last(select 'History of Status' where 'Cow table' = i);
    (create 'History of Status').(
        Date := today();
        'Name Cow' := i.Name;
        'New Status' := text(i.Status);
        'Cow table' := i
    );
    previousStatus.('End Date' := today())
    

    In the above code it is supposed that the 'table' you mentioned is a subtable of the Cow table (named 'History of Status').

    Can you please share with us the code you use inside the trigger after update field in order to make the changes directly there?

      • jesusfreakk_alex
      • 4 mths ago
      • Reported - view

       thank you for your reply.
      I actually found the answer. I used the following code:

      let newName := Name;
      let newStatus := Status.Status;
      let newHistory := (create 'Cow Status History');
      newHistory.(Date := now());
      newHistory.('Name Cow' := newName);
      newHistory.('New Status' := newStatus);
      let newUpdate := (select 'Cow Status History')[newName = 'Name Cow' and 'Date Status End' = null and Id != newHistory.Id];
      newUpdate.('Date Status End' := now())

Content aside

  • Status Answered
  • 4 mths agoLast active
  • 2Replies
  • 50Views
  • 2 Following