Copy Required custom field Value from epic to story while creating ticket

Abyakta Lenka June 13, 2018

Customfield_102090 is a required field.

My Requirement is :

when i am creating a story then Customfield_102090 value from Epic should copy to the story .

What i am facing :

since the field is required when i am creating a story it fails because my groovy copy script is in post function.

how can i bypass Required field either by copying the field or any other method .Any help is helpful .

Let me know if you want to know more info .

3 answers

2 accepted

1 vote
Answer accepted
Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
June 14, 2018

@Mark Markov Why do you need to make that field required if you dont put it on the screen and pass the value in the postfunction?

Other solution is to make an option default in the customfield and set it required in Field Configuration. This way you dont need to put it on the screen and the script postfunction should work without problems.

If you need it, here is the Script Postfunction to copy the value from an epic. You should put it on the last position in postfunctions on create transition.

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def epicLink = customFieldManager.getCustomFieldObject(10102L)
def epic = issue.getCustomFieldValue(epicLink) as Issue
def customField = customFieldManager.getCustomFieldObject(10110L) // Your CF
def cfEpicValue = epic.getCustomFieldValue(customField)

if (epic) {
    issue.setCustomFieldValue(customField, cfEpicValue)
    issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
Abyakta Lenka June 14, 2018

@Alejandro Suárez - TecnoFor we cannot make any defaults also , if we make dafault value then user will not fill the right value and technically there is no restriction if we place the default value.

 

I am trying the solution @Mark Markov gave , if that works good else i will remove the required restriction in field config and put it in the validator .

 

Abyakta

Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
June 15, 2018

@Abyakta Lenka sorry for triple posting, i'm not sure what happened :/

The problem with that behaviour is that if you have Epic Link preseted (by clicking "plus" simbol in the epic issue) it wont work. If you want to work both ways here is the code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()

def epicLink = getFieldById("customfield_10102")
def epicLinkId
if (epicLink.getFormValue()){
epicLinkId = epicLink.getFormValue().toString().replaceAll("key:", "")
} else {
epicLinkId = getFieldById(getFieldChanged()).getValue().toString().replaceAll("key:", "")
}
def epicIssue = issueManager.getIssueObject(epicLinkId)
def cf = customFieldManager.getCustomFieldObject(102090L)
def cfValue = epicIssue.getCustomFieldValue(cf) as Option
if (cfValue) {
getFieldById("customfield_102090").setFormValue(cfValue.optionId)
}
0 votes
Answer accepted
Mark Markov
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.
June 13, 2018

Hello @Abyakta Lenka

You can fill form value via Scriptrunner Behaviours and method setFormValue

getFieldById(getFieldChanged()).setFormValue(valuefromepic

 

Abyakta Lenka June 13, 2018

@Mark Markov 

Thanks for answering 

But i didnt get your point .

Should i go to Behaviours and add a new Behaviour ?

is this the only way ? i am little unfamiliar with Behaviour .

if you can explain that will help .

Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
June 14, 2018

@Mark Markov Why do you need to make that field required if you dont put it on the screen and pass the value in the postfunction?

Other solution is to make an option default in the customfield and set it required in Field Configuration. This way you dont need to put it on the screen and the script postfunction should work without problems.

If you need it, here is the Script Postfunction to copy the value from an epic. You should put it on the last position in postfunctions on create transition.

 

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def epicLink = customFieldManager.getCustomFieldObject(10102L)
def epic = issue.getCustomFieldValue(epicLink) as Issue
def customField = customFieldManager.getCustomFieldObject(10110L) // Your CF
def cfEpicValue = epic.getCustomFieldValue(customField)

if (epic) {
issue.setCustomFieldValue(customField, cfEpicValue)
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
Mark Markov
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.
June 14, 2018

Hello @Abyakta Lenka

Yes you should add new behaviour, add mapping to your project and issue types.

Behaviuors can fill fields right on form depends on other fields. Here is example for your case:

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

if (getActionName() == "Create Issue"){
def epicKey = getFieldById(getFieldChanged()).getValue() as String
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def customField = customFieldManager.getCustomFieldObject("customfield_102090")
Issue epicissue = issueManager.getIssueObject(epicKey.substring(4))
getFieldById("customfield_102090").setFormValue(epicissue.getCustomFieldValue(customField))
}

 Let me explain what happens here.

First we find that we are on the Create Issue screen.

Then we get what we have in Epic Link field.

Declare customFieldManager and issueManager to work with customfield and issue objects.

Get our customField and issue object.

And last. Set value of customField on current from from Epic Issue.

That behaviour must be attached to Epic Link field:

go to behaviours -> your created behaviour and :
image.png

Abyakta Lenka June 16, 2018

Thanks for the help . i learned a new thing and now i know where to use this and how to use this .

0 votes
Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
June 14, 2018

Hi @Abyakta Lenka,

 

This should work, add it to the Task Workflow create postfunction, in the last place.

P.D.: you should check that CustomField ID, I dont think it is right (too long).

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def epicLink = customFieldManager.getCustomFieldObject(10102L)
def epic = issue.getCustomFieldValue(epicLink) as Issue
def customField = customFieldManager.getCustomFieldObject(102090L)
def cfEpicValue = epic.getCustomFieldValue(customField)

if (epic) {
issue.setCustomFieldValue(customField, cfEpicValue)
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
Eduard Diez
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.
September 18, 2018

@Alejandro Suárez - TecnoFor veo que controlas bastante, quisiera hacerte una pregunta referente a lo que habeis mencionado por aqui.

 

Yo trabajo en CLOUD, y me es complicado crear una story por groovy, tu sabrias hacerlo.

 

Realmente lo que necesito es que cada vez que creo una demanda se cree una story. He de hacerlo en una post function y no me acabo de aclarar.

 

Gracias

Suggest an answer

Log in or Sign up to answer