I would like to copy select list field value(Primary Task) to text field(ST) using scriptlistener

Vamshi Krishna Challandula
Contributor
November 18, 2021

Hi All,

I would like to copy select list field value(Parent) to text field(Sub-Task) using scriptlistener.

Event: Issue update.

Here is my script. Please help me on this.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import java.sql.Timestamp
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.event.type.EventDispatchOption

String productlist = "Product List"

def customFieldManager = ComponentAccessor.getCustomFieldManager()

 

def issueManager = ComponentAccessor.getIssueManager()


def changeHolder = new DefaultIssueChangeHolder()

Issue issue = event.issue

def product_list = customFieldManager.getCustomFieldObjectByName(productlist)
def product_value = issue.getCustomFieldValue(product_list)
log.info("productList*****")
def product_line = customFieldManager.getCustomFieldObjectByName("Product Line")

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Product List"};


if (change) {

issue.getSubTaskObjects()?.findAll { it.issueType.name in ["Sub-task"]
def subtask = ComponentAccessor.getIssueManager().getIssueByCurrentKey(it.key)
def product_line = customFieldManager.getCustomFieldObjectByName("Product Line")
issue.setCustomFieldValue(product_value)
log.info("productLinevalue*****")
product_line.updateValue(issue, subtask, new ModifiedValue(issue.getCustomFieldValue(product_line)), new DefaultIssueChangeHolder())
}

 

I'm getting below error. Please correct me.

Thanks,

Vamshi

1 answer

0 votes
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.
November 20, 2021

Hi @Vamshi Krishna Challandula

For your requirement, you can try something like this:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def issue = event.issue as MutableIssue

def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def region = customFieldManager.getCustomFieldObjectsByName('Region').first()
def sampleText = customFieldManager.getCustomFieldObjectsByName('Sample Text').first()

issue.setCustomFieldValue(sampleText, region.getValue(issue).toString())
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.ISSUE_UPDATED, false)

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

 

Below is a print screen of the Listener configuration:-

listener_config.png

I also include a few test screens for your reference:-

1) First, when I create the issue, I select South America from the Region List.

test1.png

2) Once the issue is created, as expected, the Sample Text field is updated with the value that was selected from the Region list, i.e. South America.

test2.png

 

3) Next, I try to edit the issue and update the Region list to Asia.

test3.png

4) As expected, the Sample Text field also updates its value to Asia.

test4.png

For examples 3) and 4), I use the Edit dialog instead of inline-editing for more visibility.

Since this is using a Listener, you can also update the values directly using inline editing.

 

I hope this helps to answer your question. :)

 

Thank you and Kind Regards,

Ram

Vamshi Krishna Challandula
Contributor
November 29, 2021

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

 

Thanks for your reply!

I would like to copy select list field value(Primary Task or Main Task) to text field(Sub-Task) using scriptlistener and Event issue update. Please guide me.

Appreciate your help!

Thanks,

Vamshi

Vamshi Krishna Challandula
Contributor
December 1, 2021

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

 

Here is my script. Please help me out.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.event.type.EventDispatchOption

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def issue = event.issue as MutableIssue

def issueManager = ComponentAccessor.getIssueManager()
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def changeHolder = new DefaultIssueChangeHolder()

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find

{it.field == "Product Line"}

;

if (change) {

issue.getSubTaskObjects()?.findAll

{ it.issueType.name in ["Sub-task"] def subtask = ComponentAccessor.getIssueManager().getIssueByCurrentKey(it.key) def product = customFieldManager.getCustomFieldObjectsByName('Product List').first() def productline = customFieldManager.getCustomFieldObjectsByName('Product Line').first() issue.setCustomFieldValue(productline, product.getValue(issue).toString()) log.info("productLinevalue*****") issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.ISSUE_UPDATED, false) }


}

}

Thanks,

Vamshi

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.
December 2, 2021

Hi @Vamshi Krishna Challandula

If you want to pass the value from the Parent List to the Sub-Task text field, you can try something like this:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def issue = event.issue as MutableIssue

def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def region = customFieldManager.getCustomFieldObjectsByName('Region').first()
def sampleText = customFieldManager.getCustomFieldObjectsByName('Sample Text').first()

issue.subTaskObjects.each {
def subTaskObject = it as MutableIssue
subTaskObject.setCustomFieldValue(sampleText, region.getValue(issue).toString())
issueManager.updateIssue(loggedInUser, subTaskObject, EventDispatchOption.ISSUE_UPDATED, false)
}

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

Below is a print screen of the updated Listener configuration:-

listener_config.png


I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram 

Suggest an answer

Log in or Sign up to answer