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}
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
Great job! You did it! Thanks so much for your help!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.