Skip to main content
daniels9627
February 2, 2024
Solved

Document in Folder

  • February 2, 2024
  • 12 replies
  • 0 views

Hi All

Newbie to Appian.

Trying to find, by date & time, the latest (most recently uploaded) document in a folder; this is as far as I have got in an expression I am playing around with:

a!forEach(

  items: folder(1234, "documentChildren"),

  expression: document(fv!item, "dateCreated")

)

Any good examples to help me finish this off, or can anyone suggest how to loop through each document and return the newest?

Example Algorithm in pseudo code I would like translated to Appianese.

MostRecentlyUploaded = initial ‘zero date’

For Each Documents Creation Date

              If Documents Creation Date > MostRecentlyUploaded Then

                             MostRecentlyUploaded = Documents Creation Date

              End if

End For

Return MostRecentlyUploaded /* The most recent date */

Thanks

 Daniel

    Best answer by stefanhelzle0001

    I am not sure whether a recursive approach is the best option for this. I wrote a blog post about how to implement more "complex" algorithms in Appian.

    https://appian.rocks/2022/08/29/complex-algorithms-in-appian/

    12 replies

    stefanhelzle0001
    February 2, 2024

    Let me first ask my usual question. What do you want to achieve? We typically just dump documents to folders and store the required meta data to database. Then your question can be answered by a simple query.

    amaans4178
    February 2, 2024

    I don't know if this will work for you or not but as far as I've seen recent document will have greater document Id then previous ones, so here is the code for getting most recent uploaded document -> 

    max(
      a!forEach(
        items: folder(6025, "documentChildren"),
        expression: document(fv!item, "id"),
        
      ),
      
    )

    daniels9627
    February 2, 2024

    Thanks Amaan, this worked, nice little trick.  One step forward :[emoticon:44a8a53ad3364ea78a16c5a3229f75bb]

    amaans4178
    February 2, 2024

    Alternatively, you can adopt the method you outlined in the post using pseudo code. This approach enables you to obtain the maximum value of any type within an array with minimal adjustments.

    a!localVariables(
      local!data: a!forEach(
        items: folder(6025, "documentChildren"),
        expression: document(fv!item, "dateCreated")
      ),
      rule!DCT_getMax(
        array: local!data,
        currentMax: property(local!data, 1, null),
        currentIndex: 1,
        lengthOfArray: length(local!data)
      )
    )

     
    code for DCT_getMax -> 
    if(
      ri!currentIndex > ri!lengthOfArray,
      ri!currentMax,
      rule!DCT_getMax(
        array: ri!array,
        currentMax: if(
          ri!currentMax > ri!array[ri!currentIndex],
          ri!currentMax,
          ri!array[ri!currentIndex]
        ),
        currentIndex: ri!currentIndex + 1,
        lengthOfArray: ri!lengthOfArray
      )
    )

    result -> 

    daniels9627
    February 2, 2024

    Thanks All

    I have done the Associated and Applied Developer courses, read through most of Stefan's book, and now going through the online resources/videos/tutorials and also trying my own apps, so I am on the road! (the Great Appian Way Road).  So you will probably see me again posting questions!  

    Thanks again for your reply's!!

    stefanhelzle0001
    February 2, 2024
    read through most of Stefan's book

    Very much appreciated :-) Welcome to Appian!

    daniels9627
    February 2, 2024

    Pleasure!

    harshitb6843
    February 2, 2024

    The document ID will always be incremental as Amaan mentioned and can be relied on. But here is an alternate approach with one of my favorite functions - displayValue() using the created date attribute

    a!localVariables(
      local!documents: a!forEach(
        items: folder(5, "documentChildren"),
        expression: a!map(
          id: document(fv!item, "id"),
          dateCreated: document(fv!item, "dateCreated")
        )
      ),
      local!maxDate: max(local!documents.dateCreated),
      displayvalue(
        local!maxDate,
        local!documents.dateCreated,
        local!documents.id,
        {}
      )
    )

    daniels9627
    February 2, 2024

    Thanks Harshit, works nicely!!