Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to get the Last attachment of an Issue with Rest API

Hi guys,

 

I have a scriptrunner post function that get the list of the attachment of an issue, and I need to take the last added attachment and read the content. The problem is that I don't know ho Jira organize the attachment and the script not always get the last Attachment.

I use a For Cicle to get all the attacchment and I use the lenght of the attachment in the issue(minus 1) to get the last one attachment, but the last one that resti api get is not the last one In order of date I have added.

This is my code:

def result2 = get('/rest/api/2/issue/FWT-307?fields=attachment')
.header('Content-Type', 'application/json')
.asObject(Map)
Map mappa = result2.body.fields as Map
logger.info(mappa.attachment as String)
def attLenght = (mappa.attachment as Map).size()


if(attLenght > 0 && attLenght <= 30){ 


mappa2 = (mappa.attachment as Map).get(attLenght-1) 
logger.info("MAPPA2"+ (mappa2 as String))
idCSV = mappa2.id as String


logger.info(mappa2.id as String)   
logger.info(mappa2.content as String)
contentCSV = mappa2.content as String

 

with this logger.info at the end I check that I don't get the Last Attachment i have added but another Attachment, I don't know how Jira organize the Attachment Order.

 

How to get always the last Attachment order by Date?

 

Thanks

1 answer

Hi Erika,

I can confirm if you log out the whole attachment object which in your script is called mappa2 that you will see that each attachment has a property on it called created which shows the date the atatchmnent was created (added to the issue).

This means in your code you need to write some logic to get the cretaed properties for all the attchments and then to only read the attachment which has the most recent created date.

I do not have an example of how to do this myself, but you will use this information above as a reference guide to help create the script that you require.

Kind Regards,

Kristian

Hi Kristian,

thanks for the answer I will try.

Do you know in which way jira order the attachment? because they are not ordered by date and also not by name

 

Thanks

Hi Erika,

I am unable to say what order Jira stores the attachments in as this is handled by Jira itself and I would advise raising a support ticket with Atlassian to ask them this question as they should be able to advise on this.

Regards,

Kristian

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

Announcing Team Calendars in Confluence Data Center

Hi Community! We're thrilled to share that Team Calendars for Confluence is now a built-in feature for Confluence Data Center releases 7.11 and beyond.  A long time favorite,  Team Cale...

59 views 0 3
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you