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

Change the custom field values with attached attachment name

Lakshmi S
Contributor
August 14, 2023

Hi Team,

Is this possible? If an attachment is attached with a certain text in the attachment name update a custom text field with the attachment name?


Example: if a change request gets an attachment called peerQCxyz.xls, look for "peerQC" and set the value of a text field to peerQCxyz. If there is peerQCyz.xsl after have initial value set, that initial value can be overwritten and have the text field value peerQCyz.

Is this feasible?

3 answers

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 16, 2023 edited

Hi @Lakshmi S

For your requirement, I recommend using ScriptRunner's Listener.

You must use ScriptRunner's Custom Listener and the IssueUpdated event, triggering whenever an Attachment is added to the issue.

Below is a sample working code for your reference:-

def issue = event.issue

//Update the Field Name
def fieldName = 'Sample Text Field'

issue.update {

if (issue.attachments.size() > 0) {
def fileName = issue.attachments.last().filename
def attachmentFileName = fileName.substring(0, fileName.indexOf('.'))
setCustomFieldValue(fieldName, attachmentFileName)
} else {
setCustomFieldValue(fieldName, '')
}
}

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Listener Configuration:-

listener_config.png

Below are a couple of test screenshots for your reference:-

1. First, I created a sample Issue.

test1.png

2. Next, I am adding an attachment to it.test2.png

3. Once the Attachment is added, as expected, the Sample Text Field is updated with the attachment's file name.

test3.png

 

4. Next, I tried to add another attachment, and as expected, the Sample Text Field is also updated as expected.

test4.png

Note: once you have added the attachment, please ensure to refresh your browser to see the update take place.

 

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

Lakshmi S
Contributor
August 16, 2023

Thank you so much Ram! @Ram Kumar Aravindakshan _Adaptavist_ , its working, but the actual requirement is : 

Not blindly override but it if matches a naming convention.

Let's say I want to check for a specific attachment type and I asked folks to make attachments of that type starting with Peer........pdf, only then get the attachment name and place it in the field.

if attachment starts with QC......xsl then don't write it over to that field.

btw extension of these files don't matter. Only that a subtext is part of the attachment name

Sriram Kancharla August 16, 2023 edited

Hello, I have extended @Ram Kumar Aravindakshan _Adaptavist_'s solution to check for the regex. 



def issue = event.issue

//Update the Field Name
def fieldName = 'Sample Text Field'

Set re = New RegExp
With re
.Pattern = "^peer[\d\w]+\.pdf$"
.IgnoreCase = True
.Global = False
End With

issue.update {
if (issue.attachments.size() > 0) {
foreach(attachment in issue.attachments)
{
fileName = issue.attachments.last().filename
if(re.Test(fieldName))
{
def attachmentFileName = fileName.substring(0, fileName.indexOf('.'))
setCustomFieldValue(fieldName, attachmentFileName)
}
}
} else {
setCustomFieldValue(fieldName, '')
}
}

 

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 16, 2023 edited

Hi @Sriram Kancharla / @Lakshmi S

A simpler approach instead of adding a Regex, would be to just add another if condition as shown below:-

def issue = event.issue

//Update the Field Name
def fieldName = 'Sample Text Field'

issue.update {

if (issue.attachments.size() > 0) {
def fileName = issue.attachments.last().filename

//To filter the filename
if (fileName.toLowerCase().
startsWith('peer')) {
def attachmentFileName = fileName.substring(0, fileName.indexOf('.'))
setCustomFieldValue(fieldName, attachmentFileName)
}
} else {
setCustomFieldValue(fieldName, '')
}
 

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

Like • Sriram Kancharla likes this
Lakshmi S
Contributor
August 17, 2023 edited

Ram @Ram Kumar Aravindakshan _Adaptavist_, You are ROCK! Thanks much for your code. 

For testing, I attached peer1, peer2, peer3, etc.; the field value also changed accordingly. For testing, I deleted Peer1 from the ticket and attached it again, but the existing value didn't update to Peer1; it's still the same new value.

And if I attach the file with an upper case letter like "Peer" or "peer1", it doesn't work again.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 18, 2023

Hi @Lakshmi S

I see the problem. The attachments were not sorted, so the value is not updated accordingly.

Below is the updated working code for your reference:-

def issue = event.issue
def fieldName = 'Sample Text Field'

issue.update {
if (issue.attachments.size() > 0) {
def fileName = issue.attachments.sort { it.created }.last().filename

if (fileName.toLowerCase().startsWith('peer')) {
def attachmentFileName = fileName.substring(0, fileName.indexOf('.'))
setCustomFieldValue(fieldName, attachmentFileName)
}
} else {
setCustomFieldValue(fieldName, '')
}
}

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

The only change I have made is to modify the following line:-

def fileName = issue.attachments.last().filename

to

def fileName = issue.attachments.sort { it.created }.last().filename

Now, with this modification included, it will sort the issues by the date it was added to the issue. If any attachment is removed, it will take the last added attachment and update the field accordingly.

If all the attachments are removed, the text field will become empty and hidden.

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

Lakshmi S
Contributor
August 18, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ , IIt's currently functioning properly. I've tested it in various scenarios and it's working well.

1 vote
Mohanraj Thangamuthu
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 14, 2023

Hello, Good day. You can try the automation rule to check the file name and edit the custom field.

 

Screenshot 2023-08-15 at 10.41.15 AM.png Screenshot 2023-08-15 at 10.41.19 AM.png Screenshot 2023-08-15 at 10.41.22 AM.png

0 votes
Reshma Begum {Appfire}
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 16, 2023

Hi @Lakshmi S ,

Using JMWE's Event Based Actions you can add the Event-based action 

1. Select the Event: Issue Field Value Changed
2. Select Attachment under the Fields to monitor section 

2023-08-16_19-32-07.png

Under Post Functions, add the Set Issue Fields Post Function and select the text field that you want to update and add the below script:

if (!!issue.get("attachment"))
issue.get("attachment")?.first()?.filename.takeBefore(".")

2023-08-16_19-29-45.png

When an attachment is added, you will see the value being updated with the file name without the extension.

2023-08-16_19-35-51.png

If there is more than one attachment, the script takes the first attachment into consideration.

Hope this helps!

Thanks,

Reshma

Lakshmi S
Contributor
August 16, 2023

Thank you so much @Reshma Begum {Appfire} _ , its working, but the actual requirement is : 
Not blindly override but it if matches a naming convention.

Let's say I want to check for a specific attachment type and I asked folks to make attachments of that type starting with Peer........pdf, only then get the attachment name and place it in the field.

if attachment starts with QC......xsl then don't write it over to that field.

btw extension of these files don't matter. Only that a subtext is part of the attachment name

Reshma Begum {Appfire}
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 16, 2023

Hi @Lakshmi S ,

Thank you for giving it a try!

To check for a specific attachment name, modify the script as shown below

if (!!issue.get("attachment") && 
issue.get("attachment")?.last()?.filename.startsWith("peer") == true)

issue.get("attachment")?.last()?.filename.takeBefore(".")

 Please let us know if this meets your requirement.

Thanks,

Reshma

Lakshmi S
Contributor
August 17, 2023

Hi @Reshma Begum {Appfire} ,

I tried it using JMWE.

I attached peer1, peer2, peer3, etc.; the field value also changed accordingly.

But, I deleted Peer1 from the ticket and attached it again, but the existing value didn't update to Peer1; it's still the same old value.

And if I attach the file with and upper case letter file like "Peer" or "Peer1", it doesn't work again.

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, team '25, conference, certifications, bootcamps, training experience, anaheim ca,

Want to make the most of Team ‘25?

Spend the day sharpening your skills in Atlassian Cloud Organization Admin or Jira Administration, then take the exam onsite. Already ready? Take one - or more - of 12 different certification exams while you’re in Anaheim at Team' 25.

Learn more
AUG Leaders

Upcoming Jira Events