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

Script for sorting values in a customfield

Jeremy Upright April 30, 2021

I have a single select customfield that loads new values from a csv file on a schedule (midnight each night).  When the values are loaded they show at the top of the list, so I am looking for a separate script that I can run on a schedule (1AM each morning) that will sort the values in the aforementioned customfield.  Has anyone had any success doing something like this?

 

For reference, here is the entire script that we have configured to run as a job in Scriptrunner:

//JIRA_Pcode PROD
import java.text.SimpleDateFormat
import java.util.regex.*
import org.apache.commons.io.FileUtils;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.atlassian.jira.component.ComponentAccessor;
import com.csvreader.CsvReader;
def date = new Date()
def yesterday = date - 1
def sdf = new SimpleDateFormat("yyyyMMdd")
def a = sdf.format(date)
def b = sdf.format(yesterday)
def c = "//[hostname]/pjira/cappm_jira_clarityProject_" + b + ".csv"

def issue = ComponentAccessor.getIssueManager().getIssueObject("SCRUM-3")
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Project P-Code")
def fieldConfig = customField.getRelevantConfig(issue)
def optionsManager = ComponentAccessor.getOptionsManager()
def options = optionsManager.getOptions(customField.getRelevantConfig(issue))

CsvReader products = new CsvReader(c);
products.readHeaders();
while (products.readRecord())
{
def id = products.get("ID");
def project = products.get("Project");
def time_entry = products.get("Open for Time Entry");
def clarity = products.get("Clarity Project");
def action = products.get("Action");
optionsManager.getOptions(fieldConfig).each {
if((it.value == clarity) && (time_entry.equalsIgnoreCase('Y')) && (action.equalsIgnoreCase("UPDATE"))){
optionsManager.enableOption(it)
return}
else if((it.value == clarity) && (time_entry.equalsIgnoreCase('N')) && (action.equalsIgnoreCase("UPDATE"))){
optionsManager.disableOption(it)
return}
else if((it.value.startsWith("P000")) && (it.value.substring(0, 9) == id) && (it.value != clarity) && (time_entry.equalsIgnoreCase('N')) && (action.equalsIgnoreCase("UPDATE"))){
optionsManager.disableOption(it)
optionsManager.setValue(it, clarity)
return}
else if((it.value.startsWith("P000")) && (it.value.substring(0, 9) == id) && (it.value != clarity) && (time_entry.equalsIgnoreCase('Y')) && (action.equalsIgnoreCase("UPDATE"))){
optionsManager.enableOption(it)
optionsManager.setValue(it, clarity)
return}
}
if (action.equalsIgnoreCase("INSERT") && (time_entry.equalsIgnoreCase('Y')))
{
if (optionsManager.getOptions(fieldConfig).value.contains(clarity))
{
log.error (clarity + ' ----' + " option exists")
}
else optionsManager.createOption(fieldConfig, null, null, clarity)
log.error (clarity + '----' + "value added")
}
else if (action.equalsIgnoreCase("INSERT") && (time_entry.equalsIgnoreCase('N')))
{
if (optionsManager.getOptions(fieldConfig).value.contains(clarity))
{
log.error (clarity + ' ----' + " disabled option exists")
}
else optionsManager.createOption(fieldConfig, null, null, clarity)
log.error (clarity + '----' + "value added")
optionsManager.getOptions(fieldConfig).each{
if ((it.value == clarity) && (time_entry.equalsIgnoreCase('N'))
&& (action.equalsIgnoreCase("INSERT"))){
optionsManager.disableOption(it)}}
}
}
products.close();
File srcFile = new File(c)
File destDir = new File ("//[hostname]/pjira/jiradata/Spark Integration/processed")
FileUtils.moveFileToDirectory(srcFile, destDir, true)

 

We tried incorporating the following, but it causes the script to run for an extremely long time.

optionsManager.getOptions(fieldConfig).each{options.sortOptionsByValue(null)}

Also open to any recommendations on improvements to the script.

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.5.4
TAGS
AUG Leaders

Atlassian Community Events