Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

attachment id order and timestamp

Almu February 28, 2022

Hey guys,

Jira users want to see the list of attachments based on reverse time. My code is

if (attachments){
for (attachment in attachments.reverse()) {

if(count>0){
text+='\n'
}

text+= attachment.getCreated().format('dd/MM/yyyy HH:mm:ss') + ": " + attachment.getId().toString()

count+=1
}

def rendererManager = ComponentAccessor.getComponent(RendererManager.class)
def fieldLayoutItem = ComponentAccessor.getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem("attachment")
def renderer = rendererManager.getRendererForField(fieldLayoutItem)

renderer.render(text, null)
}

 

The result is:

Timestamp                   Id

------------                  -----

22/02/2022 21:49:07: 17354
22/02/2022 21:49:12: 17353
22/02/2022 21:49:09: 17352

 

Which means the order is based on attachment id. Please, how can I order them based on creation time? Thanks!

Almu

 

2 answers

1 accepted

0 votes
Answer accepted
Almu February 28, 2022

Finally, I got it

def attachments = ComponentAccessor.getAttachmentManager().getAttachments(issue)
def latestAttachment = attachments.findAll().sort { it.created }.reverse()


def count=0
def text=""

if (attachments){
for (attachment in latestAttachment) {

if(count>0){
text+='\n'
}

text+= attachment.getCreated().format('dd/MM/yyyy HH:mm:ss') + ": " +  attachment.getId().toString() 

count+=1
}

def rendererManager = ComponentAccessor.getComponent(RendererManager.class)
def fieldLayoutItem = ComponentAccessor.getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem("attachment")
def renderer = rendererManager.getRendererForField(fieldLayoutItem)

renderer.render(text, null)
}

Sachin Dhamale
Community Champion
February 28, 2022

Glad to hear that

0 votes
Sachin Dhamale
Community Champion
February 28, 2022

@Almu ,

 

Are you expecting the result like this?

 

22/02/2022 21:49:07: 17354

22/02/2022 21:49:09: 17352

22/02/2022 21:49:12: 17353

Almu February 28, 2022

Hi Sachin,

Thanks for your swift reply.

Actually in reverse time:

22/02/2022 21:49:12: 17353

22/02/2022 21:49:09: 17352

22/02/2022 21:49:07: 17354

Cheers,

Almu

Sachin Dhamale
Community Champion
February 28, 2022

@Almu ,


Normally the order of attachment creation should match with the attachment ID.

I checked some of the attachment and their ID is matching with creation date order.

 

Regarding the Groovy Script - I was thinking cant we store this date timestamp information in List and sort it down in reverse order like we do the with numbers

Almu February 28, 2022

Hi Sachin,

Thanks again for your interest. Finally, I got it with

def latestAttachment = attachments.findAll().sort { it.created }.reverse()

Cheers,

Almu

Like Sachin Dhamale likes this

Suggest an answer

Log in or Sign up to answer