ScriptRunner Behavior - Setting value of user picker

AbrahamA
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.
November 29, 2021

Hello All

I have to read user picker field from 1 issue and set it in other. I am getting error in step-3 and not sure if step-4 is required, if so how to do it.

 

Step-1: Getting other issue to read user info from current issue. In current  issue other issue is picked in a issue picker

def relatedIssue = ComponentAccessor.issueManager.getIssueByCurrentKey(issuePickerFieldValue as String)

 

Step-2: Getting user field from other issue, if not set in current issue

def relatedCustomUserFieldValue = relatedIssue.getCustomFieldValue("My User")

 

Step-3: Populate issue picker in the current issue ( Error on line 3)

def myPM = getFieldByName(userFieldName[0])
def pmuser = userManager.getUserByKey(relatedCustomUserFieldValue)
setCustomFieldValue(myPM,pmuser)  // Error says no matching method

 

Step-4: Need to update the current issue, is it required

I know ScriptRunner console equivalent code as below, is there equivalent for behavior?

ComponentAccessor.getIssueManager().updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)

 

 

Please let me know 

Abe

2 answers

1 accepted

0 votes
Answer accepted
Sebastian Krzewiński
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 29, 2021
0 votes
AbrahamA
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.
November 30, 2021

Hello

Thanks for the response. Here is the code I wrote in the Behavior. When I select "Related Issue" in the issue picker field, I expect PM field to be populated.

PM field is not populated. I also do not see log lines in atlassian-jira.log

Please let me know if you see any issue in the code, and why I do not see log lines.

 Behavior is mapped to proper project and issuetype

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.datetime.DateTimeFormatter
import com.atlassian.jira.issue.customfields.impl.DateTimeCFType
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.jira.groovy.user.FieldBehaviours
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript
import com.atlassian.jira.event.type.EventDispatchOption


@BaseScript FieldBehaviours fieldBehaviours

// Set the log level
def log = Logger.getLogger(getClass())
log.setLevel(Level.DEBUG)
log.debug("******************** at top *****************")


// Get the product value
final issuePickerFieldName = "Related Issue"
def issuePickerField = getFieldByName(issuePickerFieldName)
def issuePickerFieldValue = issuePickerField.value
if (!issuePickerFieldValue) {
return
}

// Get reference to issue from where we need to pick PM value
def relatedIssue = ComponentAccessor.issueManager.getIssueByCurrentKey(issuePickerFieldValue as String)
if (!relatedIssue) {
log.error("Could not find issue with key $issuePickerFieldValue")
return
}
log.debug("""Selected Issue Key = ${relatedIssue.key}
Selected Issue Summary = ${relatedIssue.summary}""")


// Get the PM name to set
def customFieldManager = ComponentAccessor.customFieldManager
final userFieldName = 'PM'
def myUserField = customFieldManager.getCustomFieldObjectsByName('PM')[0]
relatedIssue.getCustomFieldValue(myUserField)
def relatedCustomUserFieldValue = (myUserField ? relatedIssue.getCustomFieldValue(myUserField) : null)


def description = "Defaulted value set from related issue ${relatedIssue.key}"

// Populate user picker field
getFieldByName(userFieldName)
.setFormValue((relatedCustomUserFieldValue as ApplicationUser).name)
.setDescription(description)

 

Suggest an answer

Log in or Sign up to answer