Scriptrunner - copy custom field date value to another custom field

Sai Grandhi May 7, 2020

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? 

5 answers

2 accepted

2 votes
Answer accepted
Sai Grandhi May 7, 2020

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.  

Jeff Tomband May 7, 2020

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

Sai Grandhi May 8, 2020

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()))
}

Jeff Tomband May 12, 2020

Hi Sai,
It worked for you after that change?

If it did great!

Otherwise feel free to ask for assistance.

Regards,
Jeff

0 votes
Answer accepted
Jeff Tomband May 7, 2020

Hi Sai,
I would say the solution would depend heavily on how you want this to occur.
The different ways this could be triggered

  • Manual (you run the script)
  • Scheduled
  • When the issue is updated

If you could specify in better detail how you want this to run it would probably be easier to assist you.

Regards

0 votes
Venkata.Potli1 April 22, 2021

@Jeff Tomband  is there a way to map the values from (date due )custom field to (Due Date)system field ?

0 votes
David Coudron October 28, 2020

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

Jeff Tomband October 28, 2020

Hi David,
What version of Jira is this on?

Regards,
Jeff

0 votes
balaji June 29, 2020

Hi @Jeff Tomband 

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!

Jeff Tomband June 29, 2020

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

balaji June 29, 2020

@Jeff Tomband  much appreciated

Waiting for your code,

 

Thanks in advance,

 

Regards

Balaji 

Jeff Tomband June 29, 2020

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.

balaji June 29, 2020

@Jeff Tomband 

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!

Jeff Tomband June 29, 2020

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

balaji June 29, 2020

Hi @Jeff Tomband 

Where should we insert the mentioned field,

 

Could you check the script, where we can find the custom field names

 

Thanks!

Jeff Tomband June 29, 2020

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

balaji June 29, 2020

@Jeff Tomband 

Thanks for the script but facing some errors

refer the screenshot.

 

Thanks!

balaji June 29, 2020

error in script 1.JPG

Jeff Tomband June 29, 2020

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.

balaji June 29, 2020

@Jeff Tomband  we are using jira server - 8.8.1

Jeff Tomband June 30, 2020

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.

balaji June 30, 2020

Untitled 1.pngUntitled 2.png

balaji June 30, 2020

Untitled 3.pngUntitled 4.png

 

Hope this would help you to understand the error,

 

Thanks!

Jeff Tomband June 30, 2020

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;
balaji June 30, 2020

@Jeff Tomband 

Thanks for your effort,

 

But still errors showingUntitled 5.pngUntitled 6.png

Jeff Tomband July 1, 2020

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.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events