How to manage a scripted field based on a Date Picker field ?

support for vo June 23, 2022

Hello Atlassian Community, I'm asking you today because I'm stuck on the script of a scripted field (I'm using ScriptRunner).

Here is my usecase :

A "Start date" (Date Picker) field is diplayed on my ticket and I have created a "Week Number" scripted field, also displayed on this ticket.
I would like to retrieve format "W25 2022" on Week Number field based on the Start date.
I have started some script on the Week Number field :

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import static java.util.Calendar.WEEK_OF_YEAR
import static java.util.Calendar.YEAR
import java.sql.Timestamp

Issue issue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()

// get Start date custom field
def date1 = customFieldManager.getCustomFieldObjectByName("Start date")

// get Start date value
def date1Value = issue.getCustomFieldValue(date1) as Date

// create Week Number custom field
def date2 = Calendar.getInstance()

// copy in Week Number the Start date value
date2.setTime(date1Value)

// transform date2 into format "WeekWW YYYY"
return (issue.getCustomFieldValue(date2) as Timestamp)?.toCalendar().get("WEEK_OF_YEAR", "YEAR")

 If anyone has any idea on this topic, it will be appreciated :)

1 answer

1 accepted

2 votes
Answer accepted
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 23, 2022

Hi @support for vo 

if you just need a value of format WeekWW YYYY, you can use this code 

 

def date1 = customFieldManager.getCustomFieldObjectByName("Start date")

// get Start date value
def date1Value = issue.getCustomFieldValue(date1)

Calendar cal = Calendar.getInstance()
cal.setTime(date1Value)


return ("Week" + cal.get(Calendar.WEEK_OF_YEAR) + cal.get(Calendar.YEAR))
support for vo June 23, 2022

Hello @Tuncay Senturk

I didn't expect such a quick answer !
I have implemented your code and it works perfectly, that's exactly what I wanted to do !

Thanks a lot !!

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 23, 2022

I'm glad that it worked! :D 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events