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
Discussion posts and replies are publicly visible
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.
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"), ), )
Thanks Amaan, this worked, nice little trick. One step forward :
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) ) )
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 Thanks for confirming what you were looking for & If you are willing please hit 'Verify answer'.
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/
Certainly, I'm also uncertain, and I attempted to implement binary search in Appian using a recursive approach. Surprisingly, this consistently takes more time than a straightforward linear search. I'm puzzled by this because, as we understand, binary search typically has a time complexity of log(n), while linear search has a time complexity of O(n). Could it be attributed to calling an expression rule or passing substantial data through RI(s) multiple times?and yes I'm gonna read that. Your blogs, as well as Harshit's blogs, are rich in information and contain sharp details.
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!!
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 said:read through most of Stefan's book
Very much appreciated :-) Welcome to Appian!