Responsive image

Automatically assign Epic Link based on custom field

Hello!

In this article I'll show how You can change 'Epic Link' field based on another custom field. An additional condition is the 'Epic Link' is read-only. The solution uses the “Behavior” ScriptRunner and JavaScript injection.

Basic custom field

Create a new 'select list' custom field for the base value. Go to 'Administration->issues->custom fields->add custom field'. In my example the field name is 'ELEMENT LIST' with options: 'TEST1, TEST2, TEST3'. Set the screens used in Your project for the 'Story' issue type.

Custom Field

Behaviour

The next step is to create a 'behaviour' rule for the 'Epic Link' field and 'ELEMENT LIST' field. Go to 'Administration->add-on->Behaviours', type name for new the rule and click 'Add' button.

Behaviour

Set the mapping for Your project and the issue type. In example we map the rule on 'DEMO' project and 'Story' issue type.

Mapping

Then click on operation link 'Fields'. In the form add both the fields 'Epic Link' and 'ELEMENT LIST'. For the field 'Epic Link' turn-on switch 'Readonly'. This will prevent users from editing field access.

Epic Link

For the field 'ELEMENT LIST' write next script:

def myTestField = getFieldByName("ELEMENT LIST")
def simpleElink = getFieldById("customfield_10100")

switch(myTestField.getValue()){
    case("TEST1"):
     simpleElink.setFormValue("DEMO-42")
    break
    case("TEST2"):
     simpleElink.setFormValue("DEMO-43")
    break
    case("TEST3"):
     simpleElink.setFormValue("DEMO-44")
    break
}

Base Field

JavaScript

If in issue change the value for the field 'ELEMENT LIST', it should automatically set the corresponding value in the 'Epic Link'. But unfortunately, 'Behaviours' can't set an empty value for the 'Epic Link'. Thus, to clear the value in 'Epic Link' we use javascript injection in the description field ( 'Behaviours' also inject javascript ). Only to map our script to the project and issue type we need add an additional field configuration scheme. Go to 'Administration->Issues->Field configuration', click 'Add field configuration'. In example 'FC for epicLink'.

FC

Click 'Configure' action, find field 'ELEMENT LIST' and click appropriate 'Edit' link.

Script

In description field type javascript:

<script>
document.getElementById('customfield_10401').addEventListener("click", function (){
var f = $( "#customfield_10401 option:selected" ).text();
console.log("Clear epicLink:", f);
               if ( f != "TEST1" && f != "TEST2" && f != "TEST3" ){
                          $("#customfield_10100-field").val("");
                          $("#customfield_10100").val("")
               }
});
</script>

Next, we need to add a new scheme to associate the field configuration with the issue type. Go to 'Administration->Issues->Field configuration scheme', click 'Add field configuration scheme'. In example it is 'FCS EpicLink'. Click 'Configure' action then click 'Associate an issue type with a field configuration' in form select the issue type 'Story' and 'FC for epicLink'.

FCS

At the last stage, you need to change the scheme for the project. Open the project administrator panel, select 'Field' item then click 'Actions'->'Use different scheme' and set scheme 'FCS EpicLink'. This is it. If you create a new issue and change the value for 'ELEMENT LIST' the field 'Epic Link' will automatically change, but you cannot manually change the 'Epic Link'.

test1

test2

Have a nice day!

Link Name Created
X0QNRJJK.PNG CustomField.PNG 2019-12-20 11:10:21
TCWJRAVN.PNG Behaviour.PNG 2019-12-20 11:20:38
GLKQITXH.PNG Mapping.PNG 2019-12-20 11:24:11
0NMCIRCE.PNG EpicFiled.PNG 2019-12-20 11:34:00
TGFTRSDF.PNG BaseField.PNG 2019-12-20 11:34:04
O8NSPOK5.PNG Script.PNG 2019-12-20 11:50:43
XGNZTAXX.PNG FCS.PNG 2019-12-20 11:50:52
WEIGSZSW.PNG FC.PNG 2019-12-20 11:50:56
KTC8RL6D.PNG testA.PNG 2019-12-20 12:04:38
37ONQZAR.PNG TestB.PNG 2019-12-20 12:04:43
ECPHOCRF.png 184_Jira-512.png 2019-12-20 12:19:47
Files
0 0 0 0 0

Created:2019-12-20 11:03:46
Viewed:130

User
Time

Comment body

code Guest comment will be added after moderating
Send