Copy a jira software field into a servicedesk customfield

Lukáš Krajči November 7, 2019

Hi, I am jira dmin in our company and I am not really good at coding, so I have a "main" issue with key ESMOSD-xxxx and then I have a linked issues, which are some kind of "sub-tasks" but not sub-tasks exactly... And i need to check if the linked issue have key like "ESMOSV-xxxx" and if so, I need to copy the Assignee field of the linked issue into customfield named "Technik" in original servicedesk field "ESMOSD-xxxx"

 

I assembled some groovy code to be executed via workflow transiton, but it is just a hybrid from multiple web pages:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants

// the name of the field to copy
final String fieldNameToCopy = "Assignee"

// leave blank to copy from the last linked issue (regardless the link type)
final String issueLinkTypeName = "Vykazovanie"

def fieldManager = ComponentAccessor.fieldManager

def fieldToCopy = fieldManager.allAvailableNavigableFields.find { it.name == fieldNameToCopy }
if (!fieldToCopy) {
log.info "Could not find field with name $fieldNameToCopy"
return
}

def linkedIssues = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id)
if (!linkedIssues) {
log.info "There are no linked issues"
return
}

if (issueLinkTypeName && !(issueLinkTypeName in linkedIssues*.issueLinkType*.name)) {
log.info "Could not find any issue, linked with the $issueLinkTypeName issue type"
return
}

def linkedIssue = issueLinkTypeName ?
linkedIssues.findAll { it.issueLinkType.name == issueLinkTypeName }.last().destinationObject :
linkedIssues.last().destinationObject

def fieldToCopyId = fieldToCopy.id
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def myCustomField = customFieldManager.getCustomFieldObject("customfield_13600")

switch (fieldToCopyId) {
case fieldManager.&isCustomFieldId:
def customField = ComponentAccessor.customFieldManager.getCustomFieldObject(fieldToCopyId)
def linkedIssueCustomFieldValue = linkedIssue.getCustomFieldValue(customField)
issue.setCustomFieldValue(customField, linkedIssueCustomFieldValue)
break

case IssueFieldConstants.COMPONENTS:
def commonComponents = linkedIssue.components.findAll { it.name in issue.components*.name }
issue.setComponent(commonComponents)
break

case IssueFieldConstants.FIX_FOR_VERSIONS:
def commonFixedVersions = linkedIssue.fixVersions.findAll { it.name in issue.fixVersions*.name }
issue.setFixVersions(commonFixedVersions)
break

case IssueFieldConstants.AFFECTED_VERSIONS:
def commonVersions = linkedIssue.affectedVersions.findAll { it.name in issue.affectedVersions*.name }
issue.setFixVersions(commonVersions)
break

default:
myCustomField = linkedIssue[fieldToCopyId]
}


I have managed to make it working If I am copying the assignee field of linked issue into assignee field of parent issue, but If I am trying to copy it into the customfield it fails without a single word in logs. The customfield which I am trying to copy into it is type "Single user picker" if this is a problem it can be changed to text type CF... PLS help

2 answers

1 accepted

0 votes
Answer accepted
Ivan Tovbin
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 7, 2019

Hi Lukas,

Assuming this is a postfunction for your "parent" issue, here's the logic (if I understood you correctly):

1) Find an issue linked to your "parent" through "Vykazovanie" link type

2) If such issue is found, check if it belongs to ESMOSV project

3) Copy that issue's assignee to parent's "Technik" field

The code is as follows:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.user.ApplicationUser

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
MutableIssue parent = issue
List<IssueLink> vykazovanieLinks = ComponentAccessor.getIssueLinkManager().getOutwardLinks(parent.getId()).findAll{it.getIssueLinkType().getName().equals("Vykazovanie")}

if (vykazovanieLinks) {
Issue linkedIssue = vykazovanieLinks.find{it.getDestinationObject().getProjectObject().getKey().equals("ESMOSV")}?.getDestinationObject()
if (linkedIssue){
CustomField technik = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Technik")
parent.setCustomFieldValue(technik, linkedIssue.getAssignee())
ComponentAccessor.getIssueManager().updateIssue(currentUser, parent, EventDispatchOption.ISSUE_UPDATED, false)
}
}
Lukáš Krajči November 7, 2019

Hi Ivan, yes you get it exactly as I mean it... so I picked your code, inserted it in JIRA and there is one error and one warning:

Screenshot_7.pngScreenshot_8.pngScreenshot_9.png

Ivan Tovbin
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 7, 2019

Indeed, my mistake. I've updated the code above. You can ignore the warning on line 16. Also please make sure that you place this post function LAST in the post function list in your transition.

Lukáš Krajči November 7, 2019

So, I have it set exactly as you mentioned... post function is last in a row... after I tried it, nothig happened and I found this in log:

 

2019-11-07 12:33:18,007 ERROR [workflow.AbstractScriptWorkflowFunction]: *************************************************************************************
2019-11-07 12:33:18,007 ERROR [workflow.AbstractScriptWorkflowFunction]: Script function failed on issue: ESMOSD-4061, actionId: 351, file: null
java.lang.ClassCastException: com.atlassian.jira.user.DelegatingApplicationUser cannot be cast to com.atlassian.jira.issue.Issue
 at com.onresolve.scriptrunner.canned.jira.fields.editable.issue.SingleIssuePickerField.getDbValueFromObject(SingleIssuePickerField.groovy)
 at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.createValue(AbstractSingleFieldType.java:143)
 at com.onresolve.scriptrunner.canned.jira.fields.editable.AbstractTrackedSingleFieldType.super$3$createValue(AbstractTrackedSingleFieldType.groovy)
 at com.onresolve.scriptrunner.canned.jira.fields.editable.AbstractTrackedSingleFieldType.createValue(AbstractTrackedSingleFieldType.groovy:25)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:728)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:681)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:667)
 at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:217)
 at com.atlassian.jira.issue.IssueManager$updateIssue$0.call(Unknown Source)
 at Script121726.run(Script121726.groovy:18)
Lukáš Krajči November 7, 2019

Can you explain to me in simple way what is the issue? Maybe it is problem, that I am trying it on test instance of our jira, and this instance is for some reasons unstable...

Lukáš Krajči November 7, 2019

UPDATE: In production instance it is perfectly working, som it is only issue in test instance. So I really want to send you a huuge THANKS :)

0 votes
Gezim Shehu [Communardo]
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 7, 2019

You have quite a lot going on in your script.

However, here's a quick and short script to set a user custom field of a linked issue = assignee of original issue. I also implemented your required conditions/checks.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.event.type.EventDispatchOption

def issue = ComponentAccessor.getIssueManager().getIssueObject("MSD-50") //dummy issue - delete this
String[] validLinkTypes = ["Relates", "Blocks"]
List<IssueLink> allIssueLinks = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId()) // or getInwardLinks if incoming
if(allIssueLinks){
List<Issue> allLinkedIssues = new ArrayList<Issue>()
for(Iterator<IssueLink> iterator = allIssueLinks.iterator(); iterator.hasNext();){
IssueLink issueLink = (IssueLink) iterator.next()
if(validLinkTypes.contains(issueLink.getIssueLinkType().getName())){
def issueLinkIssue = issueLink.getDestinationObject() //or getSourceObject

//check issue key form. maybe better check the project key?
if(issueLinkIssue.key.contains("MSD")){
allLinkedIssues.add(issueLinkIssue)
}
}
}

if(allLinkedIssues){
def targetCf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Responsible")[0]

for(Issue linkedIssue : allLinkedIssues){
log.warn(linkedIssue.getCustomFieldValue(targetCf))
linkedIssue.setCustomFieldValue(targetCf, issue.assignee ?: null)

//Save the changes
def user = ComponentAccessor.getUserManager().getUserByKey("admin") //robot user
ComponentAccessor.getIssueManager().updateIssue(user, linkedIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
}
}
Lukáš Krajči November 7, 2019

Hi, many thx for your help, but when I insert your code into jira thre are two errors:

Screenshot_1.pngScreenshot_5.pngScreenshot_6.png

Gezim Shehu [Communardo]
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 7, 2019

Can you test and just run it once?

Also check if you have all the imports.

Lukáš Krajči November 7, 2019

Yes, imports are exactly as in your code and I run it once, this is result:

 

2019-11-07 10:16:41,546 WARN [workflow.AbstractScriptWorkflowFunction]: 2019-11-07 10:16:41,688 ERROR [workflow.AbstractScriptWorkflowFunction]: ************************************************************************************* 2019-11-07 10:16:41,699 ERROR [workflow.AbstractScriptWorkflowFunction]: Script function failed on issue: ESMOSD-4061, actionId: 351, file: null java.lang.ClassCastException: com.atlassian.jira.user.DelegatingApplicationUser cannot be cast to com.atlassian.jira.issue.Issue at com.onresolve.scriptrunner.canned.jira.fields.editable.issue.SingleIssuePickerField.getDbValueFromObject(SingleIssuePickerField.groovy) at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.createValue(AbstractSingleFieldType.java:143) at com.onresolve.scriptrunner.canned.jira.fields.editable.AbstractTrackedSingleFieldType.super$3$createValue(AbstractTrackedSingleFieldType.groovy) at com.onresolve.scriptrunner.canned.jira.fields.editable.AbstractTrackedSingleFieldType.createValue(AbstractTrackedSingleFieldType.groovy:25) at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396) at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:728) at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:681) at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:667) at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:217) at com.atlassian.jira.issue.IssueManager$updateIssue.call(Unknown Source) at Script121705.run(Script121705.groovy:32

And I deleted line with issue defining as was written in a note, because I need this to be done on current isssue - the one on which is WF transition being made.

 

Sorry for my bad english...

Cance

Suggest an answer

Log in or Sign up to answer