Post function in Jira should update a custom field based on values in other custom field at the time of creating the issues

ashleyg November 19, 2015

Post function should update a custom field(labels field) say field ABC with value "a1b1c1" to all the newly created issues that have the custom field “XYZ” set to one of the following values:

("a1b1c1","a2b2c2","a3b3c3",.......)

I read somewhere that this could be achieved using script runner post function but i am a novice in script writing.

If someone could help me out here with the complete script, i would be extremely grateful to this forum as always.

 

Regards,

Ashley

6 answers

1 accepted

1 vote
Answer accepted
Thanos Batagiannis _Adaptavist_
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 19, 2015

Ashley,

If you already use script runner then:

Go to Script Listeners -> Custom Listener

Select the project, in the Events field select Issue Created and in the Inline Script textfield add the following script:

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

Issue issue = event.issue

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def labelManager = ComponentAccessor.getComponent(LabelManager.class)
def listOfPredefinedValues = ["Collateral Optimisation",
                              "Financial Reporting",
                              "Risk Platform and FRTB",
                              "Trading Data Warehouse",
                              "Market Data",
                              "Technical Foundation",
                              "Trading and Risk Domain"]
def flag = false;
def label = 'Trading&Risk-Domain'
def cf = customFieldManager.getCustomFieldObjectByName("Project Name")
def dropDownValue = cf?.getValue(issue)

listOfPredefinedValues.each {value ->
    if (value.equals(dropDownValue?.toString())) {
        flag = true
        // If you want the label to have the same name (without spaces) with the drop down option, remove the comments
        // label = dropDownValue?.toString()?.replaceAll("\\s","")
    }
}

if (!flag)
    return

def user = event.getUser()
return labelManager.addLabel(user, issue.id, label, false)

In this case a label with the same name as the option of the dropdown field (XYZ) is added to the label custom field (ABC).

PS. The good with Atlassian answers is that there are cases that more than one options are proposed, if you don't use script runner then the choice is up to you smile.

ashleyg November 19, 2015

Many thanks for the swift reply and really appreciate that :) However i tried it in our instance but for some reason it didn't work. Let me provide you the exact details of the fields so that you could help me with the modified code. We have global Jira Labels field which we need to get populated with some value (continuous string as we know labels field does,'t allow spaces) once issue is created based on the drop down options we select for one of the custom fields here, Project Name (this is single select drop down field with few pre-configurd options. I will provide the options based on which we need the result upon issue creation:- "Collateral Optimisation", "Financial Reporting","Risk Platform and FRTB","Trading Data Warehouse","Market Data","Technical Foundation","Trading and Risk Domain" The value that needs to appear for Labels field upon issue creation is Trading&Risk-Domain If you could implement this in your script i would be extremely grateful If you think its not possible for system jira Labels field we are ready for a custom field of labels field as well, however other requirements would remain same. The drop down options for custom field Project Name are not continuous strings like Collateral Optimisation (2 words), so the script should be written to accomodate that. Looking forward to your response. Cheers, Ashley

Thanos Batagiannis _Adaptavist_
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 20, 2015

Hi Ashley I updated the script. If you want your label to be always Trading&Risk-Domain then use the script as it is. If you want your label to match with a valid (depending on the list of predefined values) dropdown option, then remove the comments. For example: Your Project Name dropdown has options ["Collateral Optimisation", "Financial Reporting", "Risk Platform and FRTB", "Trading Data Warehouse", "Market Data", "Technical Foundation", "Trading and Risk Domain", "Random Option 1"]. If the user selects "Random Option 1" then, NO label will be adde. If the user selects from dropdown the Technical Foundation option then a label will be added with the name TechnicalFoundation. PS. No need to create a custom field for the labels if you want to use the system's one (In the beginning I thought you had a custom field with labels). Please let me know if this works for you. Regards, Thanos

ashleyg November 20, 2015

And that worked like a charm :) Guys like you really add value to this forum and i really appreciate that. I had not expected such a swift and exact solution pertaining to my requirements to have come my way, but you made this possible, so big thanks to you. Will look forward to similar solutions in the future for other requirements as we are constantly trying to explore Jira in a highly customized way and script runner plugin really helps go a big deal in the journey. Cheers, Ashley

0 votes
Thanos Batagiannis _Adaptavist_
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 19, 2015

Hi Ashley, I updated the code to match with your requirements. If you decide to go with the script, please let me know if you need further assistance.

0 votes
ashleyg November 19, 2015

Thanks for the swift reply. XYZ is here a single select drop down field with pre-configured options like, a1b1c1,etc also a1b1c1 already exists as label.

0 votes
Fidel Castro
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 19, 2015

Hi Ashley,

You can do it using "Set a field as a function of other fieldspost-function of JIRA Workflow Toolbox plugin. To do it use the following configuration in transition "Create Issue" (insert it after "Creates the issue originally" post-function): 

Captura de pantalla 2015-11-19 a las 16.06.45.png

This configuration will add label a1b1c1 to field "ABC", keeping the current labels set in the field. If you want to replace any previous value with the new label, then remove prefix '+', i.e. use the following setting rule:

(a1b1c1|a2b2c2|a3b3c3)a1b1c1
0 votes
Thanos Batagiannis _Adaptavist_
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 19, 2015

And does the label "a1b1c1" already exists or you have to create it ?

0 votes
Thanos Batagiannis _Adaptavist_
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 19, 2015

Hi Ashley, Can I ask you what type is the custom field "XYZ" ?

Suggest an answer

Log in or Sign up to answer