Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Getting error attempting to update custom field value from Regex result using Scriptrunner

New to Scriptrunner.  Developing script to update custom field value from last issue comment using RegEx.  The target string format in the comment is "Ticket#123456". Getting error result when testing in Scriptrunner console

class com.riadalabs.jira.plugins.insight.common.exception.GroovyInsightException

GroovyInsightException: No such property: event for class: Script1'

Would appreciate any information on the nature of the error.

Thanks! 

import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.opensymphony.workflow.InvalidInputException
import java.util.regex.Matcher
import java.util.regex.Pattern

// Event listener - listening for the Issue Commented event

def event = event as IssueEvent
def issue = event.issue as Issue
def comment = event.getComment()
def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tgtField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "TicketNum"}
def changeHolder = new DefaultIssueChangeHolder()

// Run RegEx against last comment to find ticket number

if (comment) {
// String to be scanned
String lastcomment = comment.body

// Regex patterns
String regex1 = "Ticket#[0-9]+"
String regex2 = "[0-9]+"

// Create pattern1 object
Pattern r1 = Pattern.compile(regex1)

// Create matcher1 object
Matcher matcher1 = r1.matcher(lastcomment)
if (matcher1.find( )) {
String result1 = matcher1

// Create pattern2 object
Pattern r2 = Pattern.compile(regex2)

// Create matcher2 object
Matcher matcher2 = r2.matcher(result1)
if (matcher2.find( )) {
String result2 = matcher2
}

// Assign result2 value to custom field "TicketNum"
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), "result2"),changeHolder)
}
}

 

1 answer

1 accepted

0 votes
Answer accepted

Figured it out with some help.  Added logging (now removed) and better string manipulation and ran the script as a Listener rather than from the Scriptrunner console.  Interestingly, even though the listener works, it still errors in the console.  Script shown below with resources used to build it.

From an event listener
https://scriptrunner.adaptavist.com/latest/jira/recipes/misc/jira-service-desk.html#_from_an_event_listener

Update Custom Field Value using a “Scriptrunner for Jira” Custom Listener
https://community.atlassian.com/t5/Answers-Developer-Questions/Update-Custom-Field-Value-using-a-Scriptrunner-for-Jira-Custom/qaq-p/503643

Apache Groovy Documentation
http://docs.groovy-lang.org
http://docs.groovy-lang.org/docs/groovy-1.7.2/html/groovy-jdk/index-all.html
http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/regex/Matcher.html

import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.MutableIssue
import java.util.regex.Matcher
import java.util.regex.Pattern

def event = event as IssueEvent
def issue = event.issue as Issue
def comment = event.getComment()
def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tgtField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "Ticket Number"}
def changeHolder = new DefaultIssueChangeHolder()

// Event listener - listening for the Issue Commented event
// Run RegEx against last comment to find ticket number
// Target string format in comment body is Ticket#123456

if (comment) {
// String to be scanned
String lastcomment = comment.body
String regex1 = "Ticket#[0-9]+"
String regex2 = "[0-9]+"

// Create pattern1 object
Pattern r1 = Pattern.compile(regex1)

// Create matcher1 object
Matcher matcher1 = r1.matcher(lastcomment)
if (matcher1.find( )) {
String result1 = matcher1.getAt([0])

// Create pattern2 object
Pattern r2 = Pattern.compile(regex2)

// Create matcher2 object
Matcher matcher2 = r2.matcher(result1)

if (matcher2.find( )) {
String result2 = matcher2.getAt([0])
result2 = result2.minus("[").minus("]")

// Assign result2 value to custom field "SSI Ticket"
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), result2),changeHolder)
}
}
}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

Why upgrade to Jira Service Management Premium?

We often have questions from folks using Jira Service Management about the benefits to using Premium. Check out this video to learn how you can unlock even more value in our Premium plan.  &nb...

170 views 0 4
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you