Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Scriptrunner - copy custom field date value to another custom field

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

1 vote
Answer accepted

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

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

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

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

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!

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

@Jeff Tomband  much appreciated

Waiting for your code,

 

Thanks in advance,

 

Regards

Balaji 

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.

@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!

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

Hi @Jeff Tomband 

Where should we insert the mentioned field,

 

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

 

Thanks!

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

@Jeff Tomband 

Thanks for the script but facing some errors

refer the screenshot.

 

Thanks!

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.

@Jeff Tomband  we are using jira server - 8.8.1

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.

Untitled 3.pngUntitled 4.png

 

Hope this would help you to understand the error,

 

Thanks!

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;

@Jeff Tomband 

Thanks for your effort,

 

But still errors showingUntitled 5.pngUntitled 6.png

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.

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

Hi David,
What version of Jira is this on?

Regards,
Jeff

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

JSM June Challenge #2: Share how your business teams became ITSM rockstars

For JSM June Challenge #2, share how your non-technical teams like HR, legal, marketing, finance, and beyond started using Jira Service Management! Tell us: Did they ask to start using it or...

194 views 6 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you