Get most recent entry from referenced app in calculation field

Comments

10 comments

  • Rainer Grabowski

    Hi Omar,

    the latest related Order (= last element in the array) you get with:

    @all of Quantity[@all of Quantity.length-1]
    

    The oldest you get with:

    @all of Quantity[0]
    

    Rainer
    rg@delos-consulting.com

    0
    Comment actions Permalink
  • Omar Bahra

    That doesn't really work because the order in the array is based on the order you added the item in
    so if you add item 1 then 2 then then @all of Quantity[@all of Quantity.length-1] = 2
    but if you add 2 then 1 then @all of Quantity[@all of Quantity.length-1]=1
    what i'm looking for is that in the order history i have date field and i want to return the most recent order regardless what was the order they were added in

    0
    Comment actions Permalink
  • Rainer Grabowski

    This should work:

    var allDate = @all of datefield;
    var maxDate = moment(@max of date).format("YYYYMMDDhhmm");
    var quant = @all of quantity;
    
    result = 0;
    for(var i = 0; i < allDate.length; i++){
    if(moment(allDate[i].format("YYYYMMDDhhmm") == maxDate){
    result +=quant[i]
    }
    };
    

    Rainer
    rg@delos-consulting.com

    0
    Comment actions Permalink
  • Omar Bahra

    Thank you Rainer, that was really helpful

    0
    Comment actions Permalink
  • Tiago Lopes de Azevedo

    I get "unexpected token { " with last post... Can someone help?

    0
    Comment actions Permalink
  • Rainer Grabowski

    Add a screenshot and a copy of your calculation code, please.

    Rainer

    0
    Comment actions Permalink
  • Tiago Lopes de Azevedo

    Here it goes! Thanks a lot!

     

    0
    Comment actions Permalink
  • Omar Bahra

    You are missing a ) at the end of the if statement before the {

    0
    Comment actions Permalink
  • Rainer Grabowski

    There's indeed a missing closing ), but not at the end of the if, but behind allDate[i] (before .format() ).

    The date in the moment() function must always be wrapped in brackets before further operations (like .format()).

    Rainer

    0
    Comment actions Permalink
  • Simon Midtgaard Guldberg

    So, the above from Rainer finds the index of allDate that has the maximum date.

    Now, how to proceed from there to find the index of allDate with the second highest date?

    splice?

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk