Forums

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

ScriptRunner Quickstart Guidance

Harvey Bennett
Contributor
February 24, 2019

I am new to ScriptRunner (Groovy) and was wondering if there was some simple guidance on how to access certain fields. I am used to programming in languages that are not object oriented so does anyone have a general list of "includes" libraries that might help me gain access to certain fields such as:

1. Access to simple system fields included out the box such summary, component, description,

2. Access for including custom fields that I create

3. The official time library used in conjunction with Groovy and Jira. ( I have tested out time earlier and it says the timestamp library is not supported)

 

The script that I am trying to write out is a "Post-function" script that checks if the summary name is "Test". If it is I want to update the description field with the the words "Test" and the summary with the words "New Summary". I have gone through some of the documentation and also browsed around for similar questions to mine but I am unsure of why exactly this is taking numerous lines of code to perform.  Most examples include about 5 libraries as well as defining variables at the top. I thought Groovy was a dynamic-typed programming language so I am confused to why I'm seeing fields defined in most examples.

 

log.info("Running")

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

String summary = issue.getSummary()

if (summary == "Test")
{
log.info("In")


}

 

Also, can someone please explain to me why when I do "log.info" NOTHING is returned in my logs??? I have even tried the example listed which is log.info("...") and nothing gets returned either.

 

 

1 answer

1 accepted

0 votes
Answer accepted
Antoine Berry
Community Champion
February 25, 2019

Hi,

In this particular case I don't think you need to import any library since you will be working on the issue object directly : 

if (issue.getSummary() == "Test"){
issue.setSummary("New summary")
issue.setDescription("Test")
}

Also, you can change the logging level in "Logging and profiling" to INFO so that you can see the info logs. Or you could use log.error or log.debug as well.

To get and update custom field value : 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.index.IssueIndexingService

def customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField cfObject = customFieldManager.getCustomFieldObject(12345)
def cfValue = issue.getCustomFieldValue(cfObject)

def cfNewValue = ...

cfObject.updateValue(null, issue, new ModifiedValue(cfValue, cfNewValue), new DefaultIssueChangeHolder())

def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
issue.store()
issueIndexingService.reIndex(issue)

Date fields in Jira should be of this type :

import java.sql.Timestamp

Regards 

Antoine Berry
Community Champion
February 27, 2019

Hi @Harvey Bennett, did that help ?

Harvey Bennett
Contributor
March 5, 2019

Sorry for the delayed response. I tried that code in my custom listener script (inline) and 2 of the lines are red. I still tried to test it anyways and it did not work. I also tried adding in those import libraries you listed below for the custom field along with that if statement logic. It does not like it. Not really sure what the issue is here....

Harvey Bennett
Contributor
March 5, 2019

I have changed my approach and decided to use Post-functions instead. I got the logging to work and I tried your code and it evaluated true when I ran that if statement. It however did not update the summary or the description. Any ideas? @Antoine Berry 

Antoine Berry
Community Champion
March 6, 2019

Hi @Harvey Bennett ,

If you are editing the code directly in the Jira interface, do not worry about the red lines. I just tried this code for updating the summary and description with that code and it worked fine. Also I can help with updating the field.

Please use the 

log.error("variable : " + variable)

regularly in the code to debug and paste the logs so we can see which line is the issue. Also advise what custom field type you are trying to update. 

Antoine

Harvey Bennett
Contributor
March 6, 2019

I finally got it. So i added issue.store() method to the bottom of where I assign new values to both the summary and description field and it finally stuck and made the changes. The only issue I am having is that it is pretty much throwing me a yellow squiggly under the method and saying I should use something else, but its not saying that its deprecated. @Antoine Berry 

Antoine Berry
Community Champion
March 6, 2019

May you please provide a screenshot ?

As I said if you are working directly in your browser it will highlight 'errors' but it will still work.

Suggest an answer

Log in or Sign up to answer