We would like to copy the date from 'Start Date' to 'Begin Date' in all the issues that are not in 'Done' status. Is there any script achieve this task?
Hi Jeff,
Thank you for your response, we would like to run it manually by Scriptrunner one time for now. Please let me know if there is a script.
Hi.
I wrote a script for you.
I haven't tested it but please give it a try and Get back to me with the results.
Please make sure you first test it on a staging environment.
I cannot remember if scriptrunner supports outputs so you might have to tinker with the returns to see the results and such.
I always run my scripts in Riada Insights script console, I prefer it.
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.util.ImportUtils;
import com.atlassian.jira.issue.index.IssueIndexingService
/************************************/
//SCRIPT SETS ALL ENTERED KEYS OR SEARCH MATCH TO DO NOT SEND
//If you enter keys (ABCD-1234) enter them as "ABCD-1","ABCD-2" etc.
def keys = [];
//if you want to use JQL to find the issues please enter it here.
// This may not support some plugin functionality (eg issueFunction).
String jql = '';
//if you're using keys to pick issues, please set this to false
boolean doSearch = true;
// when running this for the first time, set this to true.
//this will make it output the amount of issues it detected.
boolean checkNumberAffected = true;
//user to run the searches as
def username = "";
//field to take the value from
def sourceFieldId = 12345;
//field to set the value in
def targetFieldId = 12345;
/***********************************/
//to find this go to cog>issues>customfields>cog. and then hover over configure and check id in url
def targetField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(targetFieldId)
def sourceField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(sourceFieldId)
UserUtil userUtil = ComponentAccessor.getUserUtil()
IssueManager issueManager = ComponentAccessor.getIssueManager()
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
def user = userUtil.getUserObject(username)//ApplicationUser.getDirectoryUser()
ArrayList<Issue> issues = new ArrayList<>();
if(doSearch) {
SearchService.ParseResult parseResult = searchService.parseQuery(currentUser, jql)
if (parseResult.isValid()) {
def searchResult = searchService.search(currentUser, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
for (com.atlassian.jira.issue.DocumentIssueImpl issueDoc : searchResult.getIssues()) {
issues.add(issueManager.getIssueObject(issueDoc.getKey()))
}
}
}
else {
for (String key : keys) {
if (issueManager.getIssueByCurrentKey(key) != null) {
issues.add(issueManager.getIssueByCurrentKey(key));
}
}
}
long start = System.currentTimeMillis();
if(checkNumberAffected) {
return "The script would run on "+issues.size()+" issues";
}
for(Issue issue : issues){
MutableIssue mi = (MutableIssue) issue;
mi.setCustomFieldValue(targetField, sourceField.getValue(issue));
ComponentAccessor.getIssueManager().updateIssue(currentUser, mi, EventDispatchOption.DO_NOT_DISPATCH, false);
boolean wasIndexing = ImportUtils.isIndexIssues()
IssueIndexingService issueIndexService = ComponentAccessor.getComponent(IssueIndexingService.class)
issueIndexService.reIndex(issue)
if(!wasIndexing)
ImportUtils.setIndexIssues(true);
}
end = System.currentTimeMillis();
long genTime = end-start;
String outString = "Set Values for "+issues.size()+" issues.";
outString+=" in "+ genTime +"ms";
return outString;
Regards,
J
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Jeff,
Thanks for the script. I tried to fix some errors but the script didn't update the issues.
Because of the error I just changed the for loop
for (com.atlassian.jira.issue.DocumentIssueImpl issueDoc : searchResult.getIssues())
to
for (com.atlassian.jira.issue.Issue issueDoc : searchResult.getIssues()) {
issues.add(issueManager.getIssueObject(issueDoc.getKey()))
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sai,
It worked for you after that change?
If it did great!
Otherwise feel free to ask for assistance.
Regards,
Jeff
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sai,
I would say the solution would depend heavily on how you want this to occur.
The different ways this could be triggered
If you could specify in better detail how you want this to run it would probably be easier to assist you.
Regards
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Jeff Tomband is there a way to map the values from (date due )custom field to (Due Date)system field ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Jeff Tomband , I tried your script and it's work really good when I use issues Keys.
Thank's for it !
However, if I use JQL search I have an error :
"org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'DocumentIssueImpl[issueKey=ACTI-11]' with class 'com.atlassian.jira.issue.DocumentIssueImpl' to class 'com.atlassian.jira.issue.MutableIssue'"
Any Idea ?
I used really simple JQL as "project = ACTI" or "labels = test"
Best regards,
David
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Is possible to create a job that can daily(automatically scheduled) update the "PC-Status Evaluation" field with the values from "PC-Status"
PC-Status Evaluation - single select list
PC-Status - text field
Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes it is,
It's a bit more involved then what you did previously.
I'll write up a script for you on my lunch break.
Regards,
Jeff
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I forgot to ask a few details from you.
So you have text field containing a word like "Active" or such.
You have a single select list where one of the options is "Active".
The select list should then select "Active" as it's value.
What happens if the text is "asdasdasdasd" and not something that exists in the list.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Good question,
Actually team will know what to put in text field,
So there will limited users will be using the text field.
So we don't need to worry about that value that is not something which is does not exists in the single select list
Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Here you go
import com.atlassian.jira.user.util.UserUtil;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.web.bean.PagerFilter;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.util.ImportUtils;
import com.atlassian.jira.issue.index.IssueIndexingService;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
/************************************/
//SCRIPT SETS ALL ENTERED KEYS OR SEARCH MATCH TO DO NOT SEND
//If you enter keys (ABCD-1234) enter them as "ABCD-1","ABCD-2" etc.
def keys = [];
//if you want to use JQL to find the issues please enter it here.
// This may not support some plugin functionality (eg issueFunction).
String jql = '';
//if you're using keys to pick issues, please set this to false
boolean doSearch = true;
// when running this for the first time, set this to true.
//this will make it output the amount of issues it detected.
boolean checkNumberAffected = false;
//user to run the searches as
def username = "";
//field to take the value from
def sourceFieldId = 12345;
//field to set the value in
def targetFieldId = 12345;
/***********************************/
//to find this go to cog>issues>customfields>cog. and then hover over configure and check id in url
def targetField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(targetFieldId)
def sourceField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(sourceFieldId)
UserUtil userUtil = ComponentAccessor.getUserUtil()
IssueManager issueManager = ComponentAccessor.getIssueManager()
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
def user = userUtil.getUserObject(username)//ApplicationUser.getDirectoryUser()
ArrayList<Issue> issues = new ArrayList<>();
if(doSearch) {
SearchService.ParseResult parseResult = searchService.parseQuery(currentUser, jql)
if (parseResult.isValid()) {
def searchResult = searchService.search(currentUser, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
for (com.atlassian.jira.issue.DocumentIssueImpl issueDoc : searchResult.getIssues()) {
issues.add(issueManager.getIssueObject(issueDoc.getKey()))
}
}
}
else {
for (String key : keys) {
if (issueManager.getIssueByCurrentKey(key) != null) {
issues.add(issueManager.getIssueByCurrentKey(key));
}
}
}
long start = System.currentTimeMillis();
if(checkNumberAffected) {
return "The script would run on "+issues.size()+" issues";
}
for(Issue issue : issues){
MutableIssue mi = (MutableIssue) issue;
def optManager = ComponentAccessor.getOptionsManager();
def options = optManager.getOptions(targetField.getRelevantConfig(issue));
def targetVal = sourceField.getValue(issue);
def targetOption = options.getOptionForValue(targetVal, null) ;
if(targetOption!=null){
mi.setCustomFieldValue(targetField, targetOption);
ComponentAccessor.getIssueManager().updateIssue(currentUser, mi, EventDispatchOption.DO_NOT_DISPATCH, false);
boolean wasIndexing = ImportUtils.isIndexIssues()
IssueIndexingService issueIndexService = ComponentAccessor.getComponent(IssueIndexingService.class)
issueIndexService.reIndex(issue)
if(!wasIndexing)
ImportUtils.setIndexIssues(true);
}
}
end = System.currentTimeMillis();
long genTime = end-start;
String outString = "Set Values for "+issues.size()+" issues.";
outString+=" in "+ genTime +"ms";
return outString;
https://scriptrunner.adaptavist.com/latest/jira/custom-script-jobs.html
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Where should we insert the mentioned field,
Could you check the script, where we can find the custom field names
Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You set the IDs
names aren't dependable
//field to take the value from
def sourceFieldId = 12345;
//field to set the value in
def targetFieldId = 12345;
in the top section
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Balaji,
I tested the script successfully on my end.
If you could provide the detailed errors at the different locations and what version of jira you're using i might be able to assist you further.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Could you please send me any specific errors you're getting. Maybe you can see the specific errors by hovering over the red cross icon.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Could you please try this
/************************************/
//SCRIPT SETS ALL ENTERED KEYS OR SEARCH MATCH TO DO NOT SEND
//If you enter keys (ABCD-1234) enter them as "ABCD-1","ABCD-2" etc.
def keys = [];
//if you want to use JQL to find the issues please enter it here.
// This may not support some plugin functionality (eg issueFunction).
String jql = '';
//if you're using keys to pick issues, please set this to false
boolean doSearch = true;
// when running this for the first time, set this to true.
//this will make it output the amount of issues it detected.
boolean checkNumberAffected = false;
//user to run the searches as
def username = "";
//field to take the value from
def sourceFieldId = 12345;
//field to set the value in
def targetFieldId = 12345;
/***********************************/
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
//to find this go to cog>issues>customfields>cog. and then hover over configure and check id in url
def targetField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(targetFieldId)
def sourceField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(sourceFieldId)
UserUtil userUtil = ComponentAccessor.getUserUtil()
IssueManager issueManager = ComponentAccessor.getIssueManager()
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
def user = userUtil.getUserObject(username)//ApplicationUser.getDirectoryUser()
ArrayList<Issue> issues = new ArrayList<>();
if(doSearch) {
SearchService.ParseResult parseResult = searchService.parseQuery(currentUser, jql)
if (parseResult.isValid()) {
def searchResult = searchService.search(currentUser, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues.addAll(searchResult.getResults());
}
}
else {
for (String key : keys) {
if (issueManager.getIssueByCurrentKey(key) != null) {
issues.add(issueManager.getIssueByCurrentKey(key));
}
}
}
long start = System.currentTimeMillis();
if(checkNumberAffected) {
return "The script would run on "+issues.size()+" issues";
}
for(Issue issue : issues){
MutableIssue mi = (MutableIssue) issue;
def optManager = ComponentAccessor.getOptionsManager();
def options = optManager.getOptions(targetField.getRelevantConfig(issue));
String targetVal = sourceField.getValue(issue);
def targetOption = options.getOptionForValue(targetVal, null) ;
if(targetOption!=null){
mi.setCustomFieldValue(targetField, targetOption);
ComponentAccessor.getIssueManager().updateIssue(currentUser, mi, EventDispatchOption.DO_NOT_DISPATCH, false);
boolean wasIndexing = ImportUtils.isIndexIssues()
IssueIndexingService issueIndexService = ComponentAccessor.getComponent(IssueIndexingService.class)
issueIndexService.reIndex(issue)
if(!wasIndexing)
ImportUtils.setIndexIssues(true);
}
}
def end = System.currentTimeMillis();
long genTime = end-start;
String outString = "Set Values for "+issues.size()+" issues.";
outString+=" in "+ genTime +"ms";
return outString;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I left out the imports on the last response since it was just a massive block of text
import com.atlassian.jira.user.util.UserUtil;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.web.bean.PagerFilter;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.util.ImportUtils;
import com.atlassian.jira.issue.index.IssueIndexingService;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
Just add those to the top section.
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.