Forums

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

How can I set the default value of an "Apwide File Field" during a transition?

JoeBob Wauson
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 17, 2023

I'm working on a workflow that will allow tasks to get passed down an echelon for action, but only one level at a time, giving leadership the chance to edit/respond to tasks before it goes all the way down to work centers, but I'm getting kinda stuck when it comes to attachments. I know I can copy all the attachments in the post-script, but I'd like to give users the option to edit the attachments before passing them along: enter the customfield "Apwide File Field". Currently I'm using scriptrunner to pre-fill dummy fields on the transition screen, leaving the original issue un-changed. Here's an example screen capture: 

taskOutScreen.png


I've managed to set the "Tasker Description for X" fields using Scriptrunner's Behaviors and this:

 

import com.atlassian.jira.issue.Issue

def indvTaskDesc = getFieldByName("Tasker Description for Individuals")
def issue = Issues.getByKey(getIssueContext().toString())


if (!indvTaskDesc.getFormValue()) {
    indvTaskDesc.setFormValue(issue.getCustomFieldValue('Tasker Description').toString())
}

However the Apwide Attachment Field ("Attachments for Units") is being a PITA. Below is the closest solution I've found, but it doesn't actually update the form, it updates the field in the background, so I have to go to transition the task and hit cancel, and then when I go to transition again it updates the form value. 

 

import com.atlassian.jira.issue.Issue

import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.runner.customisers.PluginModule

@WithPlugin("com.apwide.document.file-field")

import com.apwide.file.api.FileManager;
import com.atlassian.jira.component.*;
import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.attachment.FileSystemAttachmentDirectoryAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def attachmentManager = ComponentAccessor.getAttachmentManager()
def attachmentDirectoryAccessor = ComponentAccessor.getComponent(FileSystemAttachmentDirectoryAccessor.class)

@PluginModule
FileManager fileManager

// Load the issue you want to update
def issueManager = ComponentAccessor.getIssueManager()
Issue issue = Issues.getByKey(getIssueContext().toString())

// find the target File Field to update
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def fileField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Attachments for Units"}
log.warn "File Field found: ${fileField}"

if (!issue.getCustomFieldValue(fileField)) {
    // check for attachments, then load them
    String fileName = ""
    List<Attachment> lastAttachments = attachmentManager.getAttachments(issue)
    if (lastAttachments.size() > 0) {
        log.warn "${lastAttachments.size()} attachments found"
        for (int i=0; i < lastAttachments.size(); i++) {
            // get next attachment
            fileName = lastAttachments[i].getFilename()
            //Attachment attachment = lastAttachments[i]
            Attachment attachment = lastAttachments.find({ attachment -> attachment.getFilename().equals(fileName) })
            log.warn "Found Attachment: ${attachment}"
           
            // get attachment binary file
            log.warn "Files found in issue folder: ${attachmentDirectoryAccessor.getAttachmentDirectory(issue).listFiles()}"
            def file = attachmentDirectoryAccessor.getAttachmentDirectory(issue)
                    .listFiles()
                    .find({ it-> it.getName().equals(""+attachment.id)})
            log.warn "File found: ${file}"


            if (file){
                // Size limit of file to upload (ex: 10Mo)
                long maxFileSize = 10*1000*1000;
                // upload the attachment binary file to File Field and get its unique id
                String fileId = fileManager.upload(fileName, new FileInputStream(file), maxFileSize)
                log.warn "FileId of newly updated file: ${fileId}"


                if (fileField) {
                    def changeHolder = new DefaultIssueChangeHolder()
                    // update customfield's value using a String containing the file id
                    fileField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fileField), fileId), changeHolder)
                    log.warn "File field successfully updated with the new fileId"
                }
            }
        }
    }
}

 

References:
https://file-field.apwide.com/doc/latest/server-data-center/java-groovy-api#Java/GroovyAPI-CopyanAttachmenttoaFileFieldcustomfield

https://docs.adaptavist.com/sr4js/7.13.0/features/behaviours/api-quick-reference?utm_source=product-help


Edit: I forgot to mention, but it's also only copying one attachment (the last one based on an alphanumeric scale running from 0-z). Haven't really started to deal with that yet, but any tips would be appreciated :)

1 answer

1 accepted

0 votes
Answer accepted
David Berclaz _Apwide_
Community Champion
August 21, 2023

Hi @JoeBob Wauson

Thanks for your question about our File Field Jira App.
You will soon get an answer from one of our File Field expert.

Have a nice day,

David

JoeBob Wauson
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 23, 2023

You can let your File Field experts know I've figured it out. I used a ScriptRunner "Custom script condition" to copy everything over to the custom fields immediately *before* the transition starts. From there I loop through the attachments adding them to fileId before finally adding it to the File Field. Code below:


import com.atlassian.jira.issue.Issue
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.runner.customisers.PluginModule

@WithPlugin("com.apwide.document.file-field")

import com.apwide.file.api.FileManager;
import com.atlassian.jira.component.*;
import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.attachment.FileSystemAttachmentDirectoryAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def attachmentManager = ComponentAccessor.getAttachmentManager()
def attachmentDirectoryAccessor = ComponentAccessor.getComponent(FileSystemAttachmentDirectoryAccessor.class)

@PluginModule
FileManager fileManager

// Load the issue you want to update
def issueManager = ComponentAccessor.getIssueManager()

// find the target File Field to update
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def fileField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Attachments for Units"}
def fileField2 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Attachments for Individuals"}
log.warn "${issue.key}: File Fields found: ${fileField} and ${fileField2}"
def file
String fileId
def changeHolder

String fileName = ""
List<Attachment> lastAttachments = attachmentManager.getAttachments(issue)
if (lastAttachments.size() > 0) {
    log.warn "${issue.key}: ${lastAttachments.size()} attachments found"
    for (i in 0 .. lastAttachments.size()-1) {
        // get next attachment
        fileName = lastAttachments[i].getFilename()
        log.warn "${issue.key} [${i}]: lastAttachments[i].getFilename(): ${fileName}"
        //Attachment attachment = lastAttachments[i]
        Attachment attachment = lastAttachments.find({ attachment -> attachment.getFilename().equals(fileName) })
        log.warn "${issue.key} [${i}]: Found Attachment: ${attachment}"
       
        // get attachment binary file
        log.warn "Files found in issue folder: ${attachmentDirectoryAccessor.getAttachmentDirectory(issue).listFiles()}"
        file = attachmentDirectoryAccessor.getAttachmentDirectory(issue)
                .listFiles()
                .find({ it-> it.getName().equals(""+attachment.id)})
        log.warn "${issue.key} [${i}]: File found: ${file}"


        if (file != null){
            // Size limit of file to upload (ex: 10Mo)
            long maxFileSize = 10*1000*1000;
            // upload the attachment binary file to File Field and get its unique id
            log.warn "${issue.key} [${i}]: current status of file (${file}) and fileId (${fileId})"
            if (!fileId) {
                fileId = fileManager.upload(fileName, new FileInputStream(file), maxFileSize)
                log.warn "${issue.key} [${i}]: fileId (${fileId})"
            } else {
                fileId = fileId + "," + fileManager.upload(fileName, new FileInputStream(file), maxFileSize)
                log.warn "${issue.key} [${i}]: fileId (${fileId})"
            }
           
            log.warn "${issue.key} [${i}]: FileId of newly updated file: ${fileId}"
        }
    }
}

// Set first custom field
log.warn "fileField = ${issue.getCustomFieldValue(fileField)}"
if (!issue.getCustomFieldValue(fileField)) {
    changeHolder = new DefaultIssueChangeHolder()
    // update customfield's value using a String containing the file id(s)
    fileField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fileField), fileId), changeHolder)
    log.warn "${issue.key}: File field successfully updated with the new fileId: ${fileId}"
}

// Set second custom field
log.warn "fileField2 = ${issue.getCustomFieldValue(fileField2)}"
if (!issue.getCustomFieldValue(fileField2)) {
    changeHolder = new DefaultIssueChangeHolder()
    // update customfield's value using a String containing the file id(s)
    fileField2.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fileField2), fileId), changeHolder)
    log.warn "${issue.key}: File field successfully updated with the new fileId: ${fileId}"
}
Like David Berclaz _Apwide_ likes this
David Berclaz _Apwide_
Community Champion
December 22, 2023

Thanks for sharing!

Suggest an answer

Log in or Sign up to answer