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

Why does my script break re-indexing?

Hi team experts! This one took some time in finding the problem, and I know that you have the solution if I can just explain it well enough to understand.

The goal is to update a Scriptrunner scripted field using a common Custom Field between two Project Types.

Project A, Type 1 and Type 2 have a common field. The scripted field is on Type 1 and uses the common field to look up the value of a field that is only on Type 2. Confused?

In the script below, the Project is called PD4. Issue Type 1 isn't addressed because that is where the scripted field is. Issue Type 2 is called Control and the common Select List is called Family.

So for each record, the scripted field is using the value of Family to match the Control Issue Type that has the same value in Family. This is defined as query. Based on those results it takes the value of "Last Event Control" and defines it as last_event.

Somewhere in between the ////// marks is the part of the script that breaks re-indexing. I am not sure of all the options out there or if there is a better way to doing what we are trying to do. Any help would be appreciated. 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.event.type.EventDispatchOption;
import java.math.*
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import java.util.Map
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import org.codehaus.groovy.control.MultipleCompilationErrorsException
import com.atlassian.jira.issue.index.IssueIndexingService;

def issueIndexManager = ComponentAccessor.getComponent(IssueIndexingService)
def issueManager = ComponentAccessor.getIssueManager()
def CustomFieldManager=ComponentAccessor.getCustomFieldManager()

//Define the output of the scripted field
def late="Yes"

//Problem Rank
def String rank = issue.getCustomFieldValue(CustomFieldManager.getCustomFieldObjectsByName("Problem Rank")[0])

//Must Apply
def must_apply = issue.getCustomFieldValue(CustomFieldManager.getCustomFieldObjectsByName("Must Apply")[0])

///////////////////////////////////////////////////////////////////////
def searchService = ComponentAccessor.getComponent(SearchService)
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)

def family = issue.getCustomFieldValue(CustomFieldManager.getCustomFieldObjectsByName("Family")[0]).toString()
def query = jqlQueryParser.parseQuery("project = PD4 AND issuetype = Control AND Family = ${family}")

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def results = searchService.search(loggedInUser, query, PagerFilter.getUnlimitedFilter())

def last_event = results.results[0].getCustomFieldValue(CustomFieldManager.getCustomFieldObjectsByName("Last Event Control")[0]).toString()
/////////////////////////////////////////////////////////////////////////

//Last Event turned into a number
int leint=0
//Must Apply turned into a number
int maint=0

if (must_apply=~"DAN0"){maint=1}
else if (must_apply=~"CERT"){maint=2}
else if (must_apply=~"DAN"){maint=3}
else if (must_apply=~"QC"){maint=4}
else if (must_apply=~"RC"){maint=5}
else if (must_apply=~"PP"){maint=6}
else if (must_apply=~"MP"){maint=7}
else {maint=0}

if (last_event=~"DAN0"){leint=1}
else if (last_event=~"CERT"){leint=2}
else if (last_event=~"DAN"){leint=3}
else if (last_event=~"QC"){leint=4}
else if (last_event=~"RC"){leint=5}
else if (last_event=~"PP"){leint=6}
else if (last_event=~"MP"){leint=7}
else {leint=-1}

// Is Must Apply earlier than Last Event
if (maint<=leint){return late}
// Is Must Apply later than Last Event
if (maint>leint){return null}

 

1 answer

1 accepted

Hi @Todd Palermo ,

I know that it's not recommended to perform JQL queries in scripted field because the JQL queries are unavailable during the lock re-index.

You should check if the index is available in your scripted field, check this documentation 

https://scriptrunner.adaptavist.com/latest/jira/scripted-fields.html#_jql_searches_in_script_fields

Hi @Italo Qualisoni _e-Core_ 

Great job! You did it! Thanks so much for your help!

Suggest an answer

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

Announcing the waitlist for Jira Work Management

Hey there Cloud Community members! We’re excited to give you the first glimpse of the new home for business teams on Jira — Jira Work Management. Jira Work Management is the next generation of J...

132 views 3 7
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