8 Jul
2020

Automatically assign Epic Link based on custom field

Category:SoftTag: , :

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.

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.

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

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.

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
}
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’.

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

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’.

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’.

Have a nice day!

3 thoughts on “Automatically assign Epic Link based on custom field

Leave a Reply

Your email address will not be published. Required fields are marked *