Is it possible to create a new ticket whenever the custom field gets updated, using Scriptrunner

Sahish November 24, 2021

Hey Guys,

Hope everyone are doing great.

I have two statuses like In Analysis and Implementation. Between this we have Implement transition screen. Whenever I am changing the status from Analysis to Implement, I will get one popup screen which has Multiple Variants and Affected Variants (dropdown) Custom fields. If I select Affected variants are some abc, efg , so immediately one issue ticket will be created with these affected variants (used post functions to create a ticket over here) and the issue will be in Implementation status. During this status, I found that other affected variants Like klm, xyz also affecting. I updated the ticket with these variants. So now the total variants are abc,efg,klm,xyz. Now my requirement is, whenever there are updates to the field "Affected Variants " , another issue ticket should be created with updated variants like klm,xyz. Is it possible to to get this requirement? If yes please advise me how to proceed further.

 

Thanks in advance,

1 answer

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 27, 2021

Hi @Sahish

To answer your question, yes, this is doable. You can do this using the Listener.

Below is a sample working code for your reference:-

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

def issue = event.issue as MutableIssue
def project = issue.projectObject
def summary = 'This is an auto created issue'
def description = 'This is an example'

def issueFactory = ComponentAccessor.issueFactory
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager
def type = customFieldManager.getCustomFieldObjectsByName('New Type').first()
def typeValue = type.getValue(issue).toString()

def issueObject = issueFactory.issue

if (typeValue != 'null') {
issueObject.setProjectObject(project)
issueObject.setSummary(summary)
issueObject.setDescription(description)
issueObject.setIssueTypeId(project.issueTypes.find { it.name == typeValue }.id)
issueObject.setReporter(loggedInUser)
issueManager.createIssueObject(loggedInUser,issueObject)
issueObject.setPriority(issue.priority)
issueManager.updateIssue(loggedInUser,issueObject, EventDispatchOption.DO_NOT_DISPATCH,false)
}

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

Below is a print screen of the Listener configuration:-

listener_configuration.png

 

Below are a few test print screens:-

1) Sample existing tickets:-

img1.png

2) Edit a newly created issue. By default, the New Type value will be None as shown in the image below:-

img2.png

3) Update the New Type list to the issue type you want to create. In the example below, the issue type selected is Task. Once the New Type list is updated, click on the update button.

img3.png

4) As expected, a new issue of Task type has been created.

img4.png

5) Open the newly created issue. The issue will have the default Summary, and Description included. Similar to the original issue, i.e. by default, the New Type field will be blank. The field needs to be updated before it can be displayed.

img5.png

 

 

I hope this helps to answer your question. :)

 

Thank you and Kind Regards,

Ram

Sahish November 29, 2021

Thanks @Ram Kumar Aravindakshan _Adaptavist_  for giving me the solution. When I tried the above code with my requirements, I got an error. Screen shot is attached for reference.

And one more thing I did not mention in the above. Actually If I update the custom field "Affected Variants " in project called "ABC", the issue ticket should create in project called say "XYZ". If I want to create like this, what sort change should I do in the code?

And after creation of issue, is it possible to get some dialog box on screen with the information "New issue ticket created". ?

Capture.PNG

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 29, 2021

Hi @Sahish

Could you also please share the updated code you are testing with, a print screen of the fields you are using, and a print screen of your Listener configuration?

Please also include a full stack trace of the error you are getting.

Thank you and Kind Regards,
Ram

Sahish November 29, 2021

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

The code is as follows:

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

def issue = event.issue as MutableIssue
def project = issue.projectObject
def summary = 'This is an auto created issue'
def description = 'This is issue'

def issueFactory = ComponentAccessor.issueFactory
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager
def type = customFieldManager.getCustomFieldObjectsByName('Affected Variants for issue').first()
def typeValue = type.getValue(issue).toString()

def issueObject = issueFactory.issue

if (typeValue != 'null') {
issueObject.setProjectObject(project)
issueObject.setSummary(summary)
issueObject.setDescription(description)
issueObject.setIssueTypeId(project.issueTypes.find { it.name == typeValue }.id)
issueObject.setReporter(loggedInUser)
issueManager.createIssueObject(loggedInUser,issueObject)
issueObject.setPriority(issue.priority)
issueManager.updateIssue(loggedInUser,issueObject, EventDispatchOption.DO_NOT_DISPATCH,false)
}

 

 

Capture 1.PNGCapture 2.PNG

Sahish November 30, 2021

Hello @Ram Kumar Aravindakshan _Adaptavist_ 

Good day,

Could you please give me the solution for above code, in execution its getting failed.

Thanks in advance,

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 3, 2021

Hi @Sahish

I don't seem to be encountering this issue in my environment.

I do not see any errors in the code you shared. It is similar to the example code that I have provided with the modification in the field name.

Could you please provide the full stack trace returned in the atlassian-jira.log file when you run this test?

Thank you and Kind Regards,
Ram

Sahish December 8, 2021

Hello @Ram Kumar Aravindakshan _Adaptavist_ 

Today also I tested the listener.

I got the error which has shown below:

Logs:

2021-12-09 08:31:30,230 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2021-12-09 08:31:30,230 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
java.lang.NullPointerException: Cannot get property 'id' on null object
at Script1.run(Script1.groovy:23)

 

Payload

{ "projects": "[SSSS] (java.util.ArrayList)", "@class": "com.onresolve.scriptrunner.canned.jira.workflow.listeners.model.CustomListenerCommand (java.lang.String)", "issue": "SSSS-4458 (com.atlassian.jira.issue.IssueImpl)", "log": "org.apache.log4j.Logger@8a8a6be2", "friendlyEventNames": "Issue Updated (java.lang.String)", "version": "6 (java.lang.Integer)", "relatedProjects": "[[key:SSSS, name:SSSS]] (java.util.ArrayList)", "name": "Custom listener (java.lang.String)", "canned-script": "com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener (java.lang.String)", "id": "7992917c-b56e-4059-929a-d9cdc252f843 (java.lang.String)", "event": "com.atlassian.jira.event.issue.IssueEvent@ba9f55cc[issue=SSSS-4458,comment=<null>,worklog=<null>,changelog=[GenericEntity:ChangeGroup][issue,148866][author,JIRAUSER301][created,2021-12-09 08:31:30.183][id,1332500],eventTypeId=2,sendMail=true,params={eventsource=action, baseurl=http://abc.int.8095},subtasksUpdated=true,spanningOperation=Optional.empty]", "bundle": "com.atlassian.jira.event.issue.DefaultIssueEventBundle@4cf8a4b", "\u00a3beanContext": "com.onresolve.scriptrunner.beans.BeanFactoryBackedBeanContext@23042c2b", "events": "[2] (java.util.ArrayList)" }

 

Hope this data helps you.

The code as follows:

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

def issue = event.issue as MutableIssue
def project = issue.projectObject
def summary = 'This is an auto created issue'
def description = 'This is issue'

def issueFactory = ComponentAccessor.issueFactory
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager
def type = customFieldManager.getCustomFieldObjectsByName('Affected Variants for issue').first()
def typeValue = type.getValue(issue).toString()

def issueObject = issueFactory.issue

if (typeValue != 'null') {
issueObject.setProjectObject(project)
issueObject.setSummary(summary)
issueObject.setDescription(description)
issueObject.setIssueTypeId(project.issueTypes.find { it.name == typeValue }.id)
issueObject.setReporter(loggedInUser)
issueManager.createIssueObject(loggedInUser,issueObject)
issueObject.setPriority(issue.priority)
issueManager.updateIssue(loggedInUser,issueObject, EventDispatchOption.DO_NOT_DISPATCH,false)
}

And one more thing I did not mention in the above. Actually If I update the custom field "Affected Variants " in project called "ABC", the issue ticket should create in project called say "XYZ". If I want to create like this, what sort change should I do in the code?

And after creation of issue, is it possible to get some dialog box on screen with the information "New issue ticket created". ?

Hoping for some better solution.

Thanks,

Sahish

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 9, 2021

Hi @Sahish

The issue is duplicated in the same project in the previous code I provided. 

If you intend to create the issue in a different project, you will need to modify the code as shown below:-

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

//Take issue from source project
def issue = event.issue as MutableIssue

def summary = 'This is an auto created issue'
def description = 'This is an example'

def issueFactory = ComponentAccessor.issueFactory
def issueManager = ComponentAccessor.issueManager
def projectManager = ComponentAccessor.projectManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager
def type = customFieldManager.getCustomFieldObjectsByName('New Type').first()
def typeValue = type.getValue(issue).toString()

//Configure Target Project
def project = projectManager.getProjectByCurrentKey('RL')

def issueObject = issueFactory.issue

if (typeValue != 'null') {
issueObject.setProjectObject(project)
issueObject.setSummary(summary)
issueObject.setDescription(description)
issueObject.setIssueTypeId(project.issueTypes.find { it.name == typeValue }.id)
issueObject.setReporter(loggedInUser)
issueManager.createIssueObject(loggedInUser, issueObject)
issueObject.setPriority(issue.priority)
issueManager.updateIssue(loggedInUser,issueObject, EventDispatchOption.DO_NOT_DISPATCH,false)
}

Please note, this sample working code is not 100% exact to your environment. Hence, you will need to make the required modifications.

In the updated code, when the change is made to the New Type list in the MOCK project, the new issue will be created in the Restrict Links (RL) project.

However, the Listener is configured for only the MOCK project, i.e., only when the New Type list field is updated in the MOCK project will trigger and create a new issue in the RL project and not vice-versa.

Below is the updated listener config:-

updated_listener_config.png

If you intend to trigger it from either project, you will need to modify the Listener configuration accordingly.

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

Sahish December 9, 2021

Hello @Ram Kumar Aravindakshan _Adaptavist_ ,

Thanks for the update. I think there is no wrong in the code, but when in execution its getting failed.

Logs:

2021-12-10 07:54:57,818 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2021-12-10 07:54:57,818 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
java.lang.NullPointerException: Cannot get property 'id' on null object
at Script14.run(Script14.groovy:28)

Code which I have given:

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

//Take issue from source project
def issue = event.issue as MutableIssue

def summary = 'This is an auto created Issue'
def description = 'This is Issue'

def issueFactory = ComponentAccessor.issueFactory
def issueManager = ComponentAccessor.issueManager
def projectManager = ComponentAccessor.projectManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager
def type = customFieldManager.getCustomFieldObjectsByName('Affected Versions for Issue').first()
def typeValue = type.getValue(issue).toString()

//Configure Target Project
def project = projectManager.getProjectByCurrentKey('SSSS')

def issueObject = issueFactory.issue

if (typeValue != 'null') {
issueObject.setProjectObject(project)
issueObject.setSummary(summary)
issueObject.setDescription(description)
issueObject.setIssueTypeId(project.issueTypes.find { it.name == typeValue }.id)
issueObject.setReporter(loggedInUser)
issueManager.createIssueObject(loggedInUser, issueObject)
issueObject.setPriority(issue.priority)
issueManager.updateIssue(loggedInUser,issueObject, EventDispatchOption.DO_NOT_DISPATCH,false)
}

Sahish December 9, 2021

and one more thing, the field "Affected Version for issue" has drop down list like say A, B, C, D, E, F etc., if the user update the above field with "D" (even though you have updated the field with A,B previously), the issue should create in project "SSSS", showing Affected Version for issue = D. It should not display A and B. 

This is the requirement @Ram Kumar Aravindakshan _Adaptavist_ 

Is this possible using scripting.? I tried with post functions and all. But it did not work.

Thanks in advance,

Sahish

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 10, 2021

Hi @Sahish

Could you please provide a print screen of the exact fields you want to use in the projects so that I can get a better picturebof your requirement and provide a better solution?

Thank you and Kind Regards,

Ram

Sahish December 10, 2021

Hello @Ram Kumar Aravindakshan _Adaptavist_ 

Capture 3.PNG

Affected Version for issue is one custom field with Select list having the above options for selecting.

Hope this data help you

Thanks,

Sahish

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 11, 2021

Hi @Sahish

If you want to set the value based on a Custom Version Picker, you will need to modify the code slightly, as shown below:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.version.Version

//Take issue from source project
def issue = event.issue as MutableIssue

def summary = 'This is an auto created issue'
def description = 'This is an example'

def issueFactory = ComponentAccessor.issueFactory
def issueManager = ComponentAccessor.issueManager
def projectManager = ComponentAccessor.projectManager
def customFieldManager = ComponentAccessor.customFieldManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def sampleVersions = customFieldManager.getCustomFieldObjectsByName('Sample Version').first()
def sampleVersionsValue = issue.getCustomFieldValue(sampleVersions) as ArrayList<Version>

//Configure Target Project
def project = projectManager.getProjectByCurrentKey('RL')

def issueObject = issueFactory.issue

if (sampleVersionsValue.size() > 0) {
issueObject.setProjectObject(project)
issueObject.setIssueType(issue.issueType)
issueObject.setSummary(summary)
issueObject.setDescription(description)
issueObject.setCustomFieldValue(sampleVersions, issue.getCustomFieldValue(sampleVersions) as ArrayList<Version>)
issueObject.setReporter(loggedInUser)
issueManager.createIssueObject(loggedInUser, issueObject)
issueObject.setPriority(issue.priority)
issueManager.updateIssue(loggedInUser,issueObject, EventDispatchOption.DO_NOT_DISPATCH,false)
}

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

You will need to ensure that both the projects have the same versions configured. If not, this will not work as expected.

Below are a few test print screens:-

1) When the Sample Version picker field is updated in the MOCK project, as shown below, a new issue is created in the RL project as shown in the 2nd print screen.

image1.png

2) The RL Project creates a new issue and updates the Sample Value picker matching the values set in the MOCK Project.

image2.png

 

I hope this helps to solve your question. :)

Thank you and Kind Regards,

Ram

Sahish December 14, 2021

Hello @Ram Kumar Aravindakshan _Adaptavist_ ,

Thanks for reaching out to me with your update.

When I run the code, again it didn't get execute. The logs are as follows:

 

2021-12-14 12:55:29,301 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2021-12-14 12:55:29,314 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
com.atlassian.jira.exception.CreateException: Error occurred while creating issue through workflow:
at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:519)
at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssueObject(DefaultIssueManager.java:616)
at com.atlassian.jira.issue.managers.RequestCachingIssueManager.createIssueObject(RequestCachingIssueManager.java:212)
at com.atlassian.jira.issue.IssueManager$createIssueObject$1.call(Unknown Source)
at Script9.run(Script9.groovy:31)
Caused by: [InvalidInputException: [Error map: [{customfield_15260=Please select the mandatory fields, components=Please select the mandatory fields, priority=Please select the mandatory fields}]] [Error list: [[]]]
at com.googlecode.jsu.util.ValidatorErrorsBuilder.process(ValidatorErrorsBuilder.java:49)
at com.innovalog.jmwe.plugins.validators.GenericValidator.validate(GenericValidator.java:133)
at com.atlassian.jira.workflow.SkippableValidator.validate(SkippableValidator.java:45)
at com.opensymphony.workflow.AbstractWorkflow.verifyInputs(AbstractWorkflow.java:1466)
at com.opensymphony.workflow.AbstractWorkflow.transitionWorkflow(AbstractWorkflow.java:1167)
at com.opensymphony.workflow.AbstractWorkflow.initialize(AbstractWorkflow.java:606)
at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:754)
at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:513)
... 4 more

 

Could you please help me on this.

Thanks in advance,

Sahish

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 14, 2021

Hi @Sahish

From the log that you have shared, I have noticed two things, i.e.:-

1. The first error is complaining about line 31 from the Listener code. If you have not made any changes to the sample code that I have shared (aside from changing the Field Name), the error is pointing to:-

issueObject.setReporter(loggedInUser)
Do you have access to both projects? Else, if you have made changes to the code, please share your updated code.
2. For the second error message, it is complaining:-
Caused by: [InvalidInputException: [Error map: [{customfield_15260=Please select the mandatory fields, components=Please select the mandatory fields, priority=Please select the mandatory fields}]] [Error list: [[]]]

Which field is the customfield_15260? Is it referring to the Version Picker? If so, are there any specific settings, e.g. validators, to ensure the field is not empty, or a field configuration to set the field is mandatory?

Please check on this field and, if possible, share a print screen of the field configuration, i.e. from the Field configuration page.

Thank you and Kind Regards,
Ram

Sahish April 19, 2022

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

Here is required information.

1. Do you have access to both projects? - Yes, I do have access for both projects.

The code is:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.version.Version

//Take issue from source project
def issue = event.issue as MutableIssue

def summary = 'This is an auto created issue'
def description = 'This is New Issue'
def issueFactory = ComponentAccessor.issueFactory
def issueManager = ComponentAccessor.issueManager
def projectManager = ComponentAccessor.projectManager
def customFieldManager = ComponentAccessor.customFieldManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def sampleVersions = customFieldManager.getCustomFieldObjectsByName('Affected Variants for Yokoten').first()
def sampleVersionsValue = issue.getCustomFieldValue(sampleVersions) as ArrayList<Version>

//Configure Target Project
def project = projectManager.getProjectByCurrentKey('SBSV')

def issueObject = issueFactory.issue

if (sampleVersionsValue.size() > 0) {
issueObject.setProjectObject(project)
issueObject.setIssueType(issue.issueType)
issueObject.setSummary(summary)
issueObject.setDescription(description)
issueObject.setCustomFieldValue(sampleVersions, issue.getCustomFieldValue(sampleVersions) as ArrayList<Version>)
issueObject.setReporter(loggedInUser)
issueManager.createIssueObject(loggedInUser, issueObject)
issueObject.setPriority(issue.priority)
issueManager.updateIssue(loggedInUser,issueObject, EventDispatchOption.DO_NOT_DISPATCH,false)
}

 

2) Which field is the customfield_15260? - It is Reproducibility.

Is it referring to the Version Picker?-No, but its mandatory. But I have given the data for that field.

 

Could you please help me on this.

Thanks in advance,

Sahish

Sahish April 19, 2022

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

We already have two post functions in the workflow for creating one new issue in two different transitions:

This post function in "Implement" transition.

yo 1.PNG

This post function in "Create new issue" transition.

yo.PNG

So, Whenever I am changing the status from Analysis to Implement, I will get one popup screen which has Multiple Variants and Affected Variants (dropdown) Custom fields. If I select Affected variants are some abc, efg , so immediately separate two new issue tickets will be created for these affected variants (used post functions to create a ticket over here) and the issue will be in Implementation status.  

So now during this status, I found that other affected variants Like klm, xyz also affecting. I updated the ticket with these variants from the edit option on the screen. So now the total variants are abc,efg,klm,xyz. Now I need to get another separate two new issue tickets for the affected variants klm and xyz. This is my requirement.

 

Is it possible ? Please advice. 

Sahish April 20, 2022

Hello @Ram Kumar Aravindakshan _Adaptavist_ ,

Could you please help me on the above issue.

Thanks in advance,

Sahish

Suggest an answer

Log in or Sign up to answer