Snippets

Adaptavist Script Console Copy Value From a Text Field To A Comment

Created by Kristian Walker
/*
* This example script  console script takes the value from a text field on an issue and copies it to a new comment on the issue.
* "All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner
* license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed or
* amended and must be included in any circumstances where the code snippet is shared by You or a third party." 
*/

// Specify the issue key here
def issueKey = '<IssueKeyHere>'

// Fetch the issue object from the key
def issue = get("/rest/api/2/issue/${issueKey}")
        .header('Content-Type', 'application/json')
        .asObject(Map)
        .body

// Get all the fields from the issue as a Map
def fields = issue.fields as Map

// Get the Custom field to get the value from
def customField = get("/rest/api/2/field")
        .asObject(List)
        .body
        .find {
                (it as Map).name == '<CustomFieldNameHere>' // Specify the custom field name here
        } as Map
        
// Save the value of the text field to a variable
def textFieldValue = (fields[customField.id] as String)?.value

// check if the value for the text field is not null
if(textFieldValue){
    
// Add a comment to the issue showing the value saved for this field. 
def commentResp = post("/rest/api/2/issue/${issueKey}/comment")
    .header('Content-Type', 'application/json')
    .body([
            body: """The value from the ${customField.name} custom field has been added to this comment and is the value below:
            
        ${textFieldValue}    
            """
    ])
    .asObject(Map)
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.