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?
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:-
Below are a couple of test screenshots for your reference:-
1. First, I created a sample Issue.
2. Next, I am adding an attachment to it.
3. Once the Attachment is added, as expected, the Sample Text Field is updated with the attachment's file name.
4. Next, I tried to add another attachment, and as expected, the Sample Text Field is also updated as expected.
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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, '')
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ram Kumar Aravindakshan _Adaptavist_ , IIt's currently functioning properly. I've tested it in various scenarios and it's working well.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello, Good day. You can try the automation rule to check the file name and edit the custom field.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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(".")
When an attachment is added, you will see the value being updated with the file name without the extension.
If there is more than one attachment, the script takes the first attachment into consideration.
Hope this helps!
Thanks,
Reshma
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.