How to Create related Issues in one Jira Project based on custom field values selected
Asked by niranjan:

Hi Team,

Could you please let me know below if it's possible in Jira.

We have two custom fields

1. Intake Type (select list)

2. Impact Location (checkboxes).

When a user select option from select list ( ex: network), and if one option is selected on checkbox (ex: US) a issue related should be created in another project (name : Infra project). If the user select two options two issues should be created, if three options selected three issues should be created and if the user select (ex: Global option) a total of 5 tickets should be created in Infra project. ![IT PROJET ERROR.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/304708i03DD4EB8C74D284E/image-size/large?v=v2&px=999 "IT PROJET ERROR.PNG")

Note: The tickets in other project should be created during a workflow transition in first Project.

Thanks


Answers:
author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@niranjan](/t5/user/viewprofilepage/user-id/1782894)

From your requirement, it seems more related to a ScriptRunner Behaviour.

Since you require the Issue to be created based on the options selected on two fields, you will need to create two separate Server-Side Behaviour configurations.

And from the Server-Side Behaviour configuration, you will need to trigger the issue creation. This can be done using ScriptRunner's HAPI.

Please confirm if this meets your requirement and I will provide an example script.

Thank you and Kind regards,

Ram

comments: author: niranjan
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) Please provide me the script which will resolve this issue.

Note: The tickets in other project should be created during a workflow transition in first Project.


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@niranjan](/t5/user/viewprofilepage/user-id/1782894)

As mentioned in my previous comment:-
> Since you require the Issue to be created based on the options selected on two fields, you will need to create two separate Server-Side Behaviour configurations.
>
> And from the Server-Side Behaviour configuration, you will need to trigger the issue creation. This can be done using ScriptRunner's HAPI.

You will need to create 2 Server-Side Behaviour configurations. The first is for the List, and the second is for the Checkbox.

Below are the example working Server-Side Behaviour code along with screenshots of the configuration for your reference:-

1. Server-Side behaviour code for the List:-  

```
import com.adaptavist.hapi.jira.issues.Issues
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def list = getFieldById(fieldChanged)
def listValue = list.value

def checkbox = getFieldByName('Checkbox')
def checkboxValue = checkbox.value.toString()

if (listValue == 'Option 1' && checkboxValue == 'Global') {
    Issues.create('EX', 'Story') {
        setSummary('Triggered via Behaviour')
        setDescription('This is an example')
    }
}
```

Below is a screenshot of the Server-Side Behaviour configuration for the List:-

![list_server_side_behaviour_config.png](https://community.atlassian.com/t5/image/serverpage/image-id/305601i0BAD4D663AC55FDD/image-size/large?v=v2&px=999 "list_server_side_behaviour_config.png")

2. Server-Side Behaviour code for the Checkbox:-  

```
import com.adaptavist.hapi.jira.issues.Issues
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def checkbox = getFieldById(fieldChanged)
def checkboxValue = checkbox.value.toString()

def list = getFieldByName('List')
def listValue = list.value

if (listValue == 'Option 1' && checkboxValue == 'Global') {
    Issues.create('EX', 'Story') {
        setSummary('Triggered via Behaviour')
        setDescription('This is an example')
    }
}
```

Below is a screenshot of the Server-Side Behaviour configuration for the Checkbox:-

![checkbox_server_side_behaviour_config.png](https://community.atlassian.com/t5/image/serverpage/image-id/305602i801BF361B2F79C50/image-size/large?v=v2&px=999 "checkbox_server_side_behaviour_config.png")

Please note that the sample working codes above are not 100% exact to your environment. Hence, you will need to make the required modifications.

If you observe the codes provided for the List's Server-Side Behaviour code, the List is initialised as:-  

```
def list = getFieldById(fieldChanged)
```

Similarly, for the Checkbox's Server-Side Behaviour code, the Checkbox is initialised as:-  

```
def checkbox = getFieldById(fieldChanged)
```

This is required to identify which field makes the change.

Also, this Behaviour configuration is only mapped to the Mock project. Hence, when the Behaviour is triggered from the Mock project, a new issue is created in the Example project.

In addition to that, in both the Behaviour codes, to create the new Issue in the Example project, I am using [ScriptRunner's HAPI](https://docs.adaptavist.com/sr4js/latest/hapi/create-issues) feature to simplify the process, i.e.:-  

```
Issues.create('EX', 'Story') {
    setSummary('Triggered via Behaviour')
    setDescription('This is an example')
}
```

I am also including a couple of test screenshots for your reference:-

1. When the is started, both the Mock and Example projects are empty without any issues, as shown in the screenshots below:-

![mock_empty.png](https://community.atlassian.com/t5/image/serverpage/image-id/305604i5B2A34FDA4148C11/image-size/large?v=v2&px=999 "mock_empty.png")

![ex_empty.png](https://community.atlassian.com/t5/image/serverpage/image-id/305603iB962027E3A694BC1/image-size/large?v=v2&px=999 "ex_empty.png")

2. Next, I am going to try and trigger the Behaviour on the Create screen of the Mock project as shown in the screenshot below:-

![test1.png](https://community.atlassian.com/t5/image/serverpage/image-id/305606iFB07DD6D29D107BA/image-size/large?v=v2&px=999 "test1.png")

4. When Option 1 is selected from the List and the Global option is selected from the Checkbox, as expect,ted a new issue is created in the Example project as shown in the screenshot below:-

![test2.png](https://community.atlassian.com/t5/image/serverpage/image-id/305607iBD9E4C090926258B/image-size/large?v=v2&px=999 "test2.png")

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram


author: Darryl Lee
body: You definitely can create ticket(s) from a ScriptRunner Post-Function:

* [Automate the Creation of an Issue in Jira](https://library.adaptavist.com/entity/basics-create-issue)

And it definitely can be conditional on Custom Field values. An elaborate example is here:

* [Post function on Custom Fields](https://community.atlassian.com/t5/Marketplace-Apps-Integrations/Post-function-on-Custom-Fields/qaq-p/679677)

In that example [@Ashraful Hasan \[Adaptavist\]](/t5/user/viewprofilepage/user-id/664761) is using a Custom Listener, although I _think_ it could be done as a Post-Function?

But I'm definitely not a ScriptRunner expert, so maybe it has to be done as a Listener. Maybe some experts could weigh in?

(Since you're on Data Center, I would consider/recommend that you do this Automation for Jira, which can easily be triggered by an Issue Creation.)


Tags: jira

jira-data-center

scriptrunner