Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Script for sorting values in a customfield

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