Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get my Scriptrunner Script to edit a field with the new value?

Jeremy Jedlicka
Contributor
January 5, 2023

I am attempting to populate two custom fields on an Issue based off the Sprint dates of a linked Story. The Sprint Start and End Date Fields are DateTime, and the Field I'm attempting to populate is just a DatePicker.

Below is the script I have written so far. When I execute it nothing happens. The Old and New dates from within the logger are still the same.

Clearly,

estStart.updateValue(null,parent,new ModifiedValue(sprintstartValue, estSOld),changeHolder)

isn't updating the value, but I'm not sure why.  Any help is really appreciated.

import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.user.util.UserUtil;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.search.SearchResults;
import com.atlassian.jira.web.bean.PagerFilter;
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("")
log.setLevel(Level.DEBUG)
log.debug("Script started")
//JQL for all stories in a Sprint
String jqlSearch = "key in (PRO4SIGHT-238)"

SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
UserUtil userUtil = ComponentAccessor.getUserUtil()
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueManager issueManager = ComponentAccessor.getIssueManager()
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def changeHolder = new DefaultIssueChangeHolder()
List<MutableIssue> issues = null

//List of variables
def
output = ""
def parent = ""
def estSOld = ""
def estSNew = ""
def sprintId = 10000
def sprint = customFieldManager.getCustomFieldObject(sprintId)
def estStartId = 21603
def estStart = customFieldManager.getCustomFieldObject(estStartId)

SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)

//Verifies that the JQL is valid
if
(parseResult.isValid()) {
 SearchResults searchResult = searchService.search(user, parseResult.getQuery(),  PagerFilter.getUnlimitedFilter())
 issues = searchResult.getResults().collect {issueManager.getIssueObject(it.id)}
 
//Starts the iteration through all stories in the JQL
issues.each {
it ->
//Pulls the Sprint Start and End Dates into two variables
    def sprintstartValue = it.getCustomFieldValue(sprint)?.startDate.first()?.toDate()
    log.debug(sprintstartValue)
    def sprintendValue = it.getCustomFieldValue(sprint)?.endDate.last()?.toDate()
    log.debug(sprintendValue)
//Searches for the parent Feature of the Story
    def links = ComponentAccessor.getIssueLinkManager().getOutwardLinks(it.getId())
      for( l in links) {  
        if(l.issueLinkType.name == "Parent of") {
          output = output + l.issueLinkType.name + ": " +  l.getDestinationObject() + "<br/>"
          parent = l.getDestinationObject()
//Gets the Old value for the Field thats getting changed
          estSOld = parent.getCustomFieldValue(estStart)
          log.debug(estSOld)
//Sets the New Value to match the Sprint Start Date
          estStart.updateValue(null,parent,new ModifiedValue(sprintstartValue, estSOld),changeHolder)
          estSNew = parent.getCustomFieldValue(estStart)
          log.debug(estSNew)
        }
      }  
  }
}

2 answers

0 votes
Vamsi Kandala
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 5, 2023

Hi @Jeremy Jedlicka

Hope the date format you are setting is 'YYYY-MM-dd'.

Thanks,
Vamsi

0 votes
Jeremy Jedlicka
Contributor
January 5, 2023

Right after posting this I found the issue..

estStart.updateValue(null,parent,new ModifiedValue(sprintstartValue, estSOld),changeHolder)

 is backwards.  It should be 

estStart.updateValue(null,parent,new ModifiedValue(estSOld, sprintstartValue),changeHolder)

 However, now I'm getting the error:


java.lang.IllegalArgumentException: Java type java.util.Date not currently supported. Sorry.

This is probably a date formatting issue right?  How can I format the Sprint Dates to work with a DatePicker?

Suggest an answer

Log in or Sign up to answer