Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Customfield trigger doesn't work -> doesnt create a clone issue (scriptrunner)

Frantisek Kust August 20, 2019

Hello,

I am looking for automation which would clone newly created ticket based on the custom field value. I am using Listeners and Clone pre-defined template with the code:

---

package com.workflow.listener

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option


// LOGGING INFO
def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
//def issueKey = issue.getKey()
log.info("Checking Code Coverage Flag for issue ${issue.getKey()}")

// TRIGGER
//issue.issueType.name == 'Bug'
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Test Coverage'}
cfValues['Test Coverage'] == '?'

--

But when a new issue is created with this custom field and value, a Clone issue is not created :( If I am using just type of issue, it is working ... 

In both cases, no issues in the log - showing the last create issue that was checked. 

2019-08-20 11:44:19,221 INFO [runner.ScriptRunnerImpl]: Checking Code Coverage Flag for issue RHV-6488

 

Any hint, suggestion, please?

3 answers

1 accepted

0 votes
Answer accepted
Frantisek Kust August 26, 2019

Thanks, Peter-Dave, but still no success :( 

I am trying to create a Custom listener, but again, no success yet.
Any idea? Thx Frank

 

--- My code --- 

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.customfields.option.Options;
import com.atlassian.jira.issue.customfields.option.Option;
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.user.ApplicationUser
import org.apache.log4j.Logger

// LOGGING INFO
def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
//def issueKey = issue.getKey()
log.info("Checking Code Coverage Flag for issue ${issue.getKey()}")


public class myClass {
// Your local CustomFieldManager
private final CustomFieldManager myCustomFieldManager;
// Class constructor requesting the injected CustomFieldManager
public myClass(CustomFieldManager injectedCustomFieldManager) {
this.myCustomFieldManager = injectedCustomFieldManager;
}

public void myMethod() {


CustomField customField = myCustomFieldManager.getCustomFieldObjectByName("Test Coverage");
Object customFieldValue = customField.getValue(issue)

// TRIGGER
if (customFieldValue.value.contains("?"))
create.newissue {
issue.summary = 'Cloned issue'
}
}
}

PD Sheehan
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.
August 26, 2019

Good luck with that one... I can't help you there.

Creating issues from scratch is more complicated and there are other better examples that what I could provide in the community. It will involve the issueFactory service.

I'd recommend you stick with the built-in "Cones and issue, and links" listener rather than a custom one. 

Frantisek Kust August 27, 2019

Thanks, Peter-Dave, I would love to stay with "Clone listener" but it is not working for some reason.

 

If I am adding this code to the Condition Field:

def originalIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(issue.getKey())
def customFieldName = "Test Coverage"
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(customFieldName)
def cfValue = event.issue.getCustomFieldValue(cf)
cfValue == '?'

---

Nothing is cloned :(

PD Sheehan
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.
August 27, 2019

Send some messages to the log and find out why it isn't working...

import com.atlassian.jira.component.ComponentAccessor
log.info "Start of clone issue listener on issue: $event.issue"
def customFieldName = "Test Coverage"
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(customFieldName)
log.info "Custom Field object found: $cf.name"
def cfValue = event.issue.getCustomFieldValue(cf)
log.info "Test Coverage Value: $cfValue"
log.ingo "Test Coverage equals '?': ${cfValue == '?'}
cfValue == '?'
Frantisek Kust August 27, 2019

Thanks. Receiving the error on line 8 

Script410.groovy: 8: expecting anything but ''\n''; got it anyway @ line 8, column 54. equals '?': ${cfValue == '?'} ^ 1 error </pre>.
Frantisek Kust August 27, 2019

Just removed the line #8 from the script and run it, but there is no information in the log.

Time (on server): Tue Aug 27 2019 15:55:25 GMT+0200 (Central European Summer Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

No logs were found for this execution.

Just from Payload:

"Test Coverage": "? (com.atlassian.jira.issue.customfields.option.LazyLoadedOption)",

PD Sheehan
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.
August 27, 2019

I type this code directly in the message box... you have you do a bit of your own syntax checking. Looks like I forgot to close the double quotes.

But your last message gives use the real reason ... you never mentioned that the Test Coverage field is a single select. Single select don't store string values, they store "Options". So try this

import com.atlassian.jira.component.ComponentAccessor
log.info "Start of clone issue listener on issue: $event.issue"
def customFieldName = "Test Coverage"
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(customFieldName)
log.info "Custom Field object found: $cf.name"
def cfValue = event.issue.getCustomFieldValue(cf)?.value
log.info "Test Coverage Value: $cfValue"
log.ingo "Test Coverage equals '?': ${cfValue == '?'}"
cfValue == '?'

Also, look in your atlassian-jira.log, sometimes logs don't appear in scriptrunner execution history.

Frantisek Kust August 27, 2019

Apologize me Peter-Dave, I am new to scripting and groovy too. I appreciate your help a lot! Thank you for patience with me.

I tried to repair after I posted it ... learning by doing. :)

With this update, it is WORKING! Great, many thanks.

Just if the Events is set to "Issue Created" - logically the listener started to generate new issue in the row. 

So is there any limit, how to set that only when issue type "bug" is created as I am duplicating bug to the task type?

Like:

def issueManager = ComponentAccessor.issueManager
issueType = issue.getIssueType();

IssueType = issue

if ((issue.getIssueType() = "bug"))

// Checking if cf Test Coverage has value "?"

import com.atlassian.jira.component.ComponentAccessor
log.info "Start of clone issue listener on issue: $event.issue"
def customFieldName = "Test Coverage"
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(customFieldName)
log.info "Custom Field object found: $cf.name"
def cfValue = event.issue.getCustomFieldValue(cf)?.value
log.info "Test Coverage Value: $cfValue"
log.ingo "Test Coverage equals '?': ${cfValue == '?'}"
cfValue == '?'

// not
} else
return false;
{
PD Sheehan
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.
August 27, 2019

issue.issueType is an issueType object ... so you'll need to compare the name.

import com.atlassian.jira.component.ComponentAccessor
if( issue.issueType.name.toLowerCase() == 'bug' ){
// Checking if cf Test Coverage has value "?"
def customFieldName = "Test Coverage"
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(customFieldName)
def cfValue = event.issue.getCustomFieldValue(cf)?.value
cfValue == '?'
} else {
   false
}
Like Frantisek Kust likes this
Frantisek Kust August 27, 2019

I did some updates, but still a lot of errors there :(  

 

import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor


UserManager userManager = ComponentAccessor.getUserManager();
MutableIssue issue = issue

// Condition that only Bug with CF and "?" will be duplicated

if(issue.issueType.name == "Bug" ){

log.info "Start of clone issue listener on issue: $issueType.name"

// Checking if cf Test Coverage has value "?"

log.info "Start of clone issue listener on issue: $event.issue"
def customFieldName = "Test Coverage"
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(customFieldName)
log.info "Custom Field object found: $cf.name"
def cfValue = event.issue.getCustomFieldValue(cf)?.value
log.info "Test Coverage Value: $cfValue"
log.info "Test Coverage equals '?': ${cfValue == '?'}"
cfValue == '?'

// not a Bug - do nothing

}else {
return false;
}
Frantisek Kust August 27, 2019

Peter-Dave - you made my day!!! Fabolous, it is working as I need exactly.

Many, many thanks. I played with the code previously and didn't realize that you already corrected it. Once again, big thanks and have a great day too!

PD Sheehan
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.
August 27, 2019

Glad I could help.

Please mark the question as answered so others can find it too.

Frantisek Kust August 28, 2019

Sure, many thanks again!

0 votes
Frantisek Kust August 28, 2019

Peter-Dave, if I will add additional condition if Component is not there, what's wrong on this code, please?

// Checking if cf Test Coverage has value "?" & Component is not Documentation
if( issue.issueType.name.toLowerCase() == 'bug' && ! issue.components*.name.contains('Documentation'){
def customFieldName = "Test Coverage"
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(customFieldName)
def cfValue = event.issue.getCustomFieldValue(cf)?.value
cfValue == '?'
} else {
false
}
PD Sheehan
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.
August 28, 2019

I see two errors in this:

1) your if block is missing a closing parens

2) your component check will fail where there are zero component because *.name will be null and therefore cannot evaluate the contains condition.

This should work:

// Checking if cf Test Coverage has value "?" & Component is not Documentation
if( issue.issueType.name.toLowerCase() == 'bug' && ! issue.components.any{it.name.contains('Documentation')}){
def customFieldName = "Test Coverage"
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(customFieldName)
def cfValue = event.issue.getCustomFieldValue(cf)?.value
cfValue == '?'
} else {
false
}
Frantisek Kust August 29, 2019

Many thanks, Peter-Dave, for those corrections. It is very good learning for me when you are making points like that to understand groovy scripting better.

Many thanx again!

0 votes
PD Sheehan
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.
August 20, 2019

Are you using this code in the "Condition" field of the Clones and Issue and links built-in script listener? 

Or somewhere else? 

Can you share more of your configuration?

Frantisek Kust August 21, 2019

HI Peter-Dave,

yes I used Clones and Issue and links built-in script and with those "Condition" in the field :

--

package com.workflow.listener

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option


// LOGGING INFO
def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
//def issueKey = issue.getKey()
log.info("Checking Code Coverage Flag for issue ${issue.getKey()}")

// TRIGGER
//issue.issueType.name == 'Bug'
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Test Coverage'}
cfValues['Test Coverage'] == '?'

---

BECAUSE when I used only "Condition" per Example "Has string custom field value equal to" - where is the code


"cfValues['Some Custom Field'] == 'Some Value'"

and my case is

cfValues['Test Coverage'] == '?'

It results in result:

2019-08-21 11:02:57,233 WARN [utils.LazyCustomFieldsMap]: Did not find requested custom field Some Custom Field on issue RHV-15021

Cancel

So it means, that I need to add into conditions 

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

And this help to fix the issue with the error in log.

But Copy of the issue was not created neither.

Maybe issue with value "?" for custom field?

Thanks, Frank

PD Sheehan
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.
August 21, 2019

I don't think the cfValues variable is available in listeners. This is something that's exposed only on workflows post function

So you'll need to fetch custom field and custom field values manually.

In the listener, if you've selected to fire on any issue events, you'll have access to the issue with event.issue.

To get the value of the custom field from that issue, call

def cfValue = event.issue.getCustomFieldValue(cf)

then, you can check for

cfValue == '?' to trigger your listener action

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events