How to initialize a custom field based on another custom field's value ? Edited

I have 3 Datetime Picker custom fields:

- Date1

- Date2

- Date3

I would like to initialize Date2 based on Date1 + 1 day and Date3 based on Date1 + 15 days.

I also need to block modification of Date2 and Date3 except by people defined in a group.

How can I do that with JIRA v6.1.5#6160-sha1:a61a0fc ?

I tried with ScriptRunner and the following code :

import com.atlassian.jira.issue
import com.atlassian.jira.component.ComponentAccessor

import java.sql.Timestamp
import java.text

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def dateMEP = customFieldManager.getCustomFieldObjectByName("Date MEP") // Date time fields require a Timestamp
def dateDG = customFieldManager.getCustomFieldObjectByName("Date début garantie") // Date time fields require a Timestamp
def dateFG = customFieldManager.getCustomFieldObjectByName("Date fin garantie") // Date time fields require a Timestamp
def df = new DateFormat()

issue.setCustomFieldValue(dateDG, new Timestamp((new Date(df.parse(dateMEP)) + 1).time))
issue.setCustomFieldValue(dateFG, new Timestamp((new Date(df.parse(dateMEP)) + 15).time))

 

Am I on the right way or does it exist another method ?

1 answer

1 accepted

I finaly found the solution... here it is :

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def dateMEP = customFieldManager.getCustomFieldObject("customfield_12102")
def dateDebGar = customFieldManager.getCustomFieldObject("customfield_12103")
def dateFinGar = customFieldManager.getCustomFieldObject("customfield_12104")

def dateMEPValue = dateMEP.getValue(issue)

issue.setCustomFieldValue(dateDebGar, dateMEPValue + 1)
issue.setCustomFieldValue(dateFinGar, dateMEPValue + 15)

Hello,

I have a similar issue and i found your solution very helpful. I have a question though. Where did you put the code? In a field or in a validation or somewhere else?

Hi,

I put the script in a file with .groovy extension.

As I didn't use the last version of Scriptrunner, I created a folder named "scripts" in the JIRA_HOME folder.

Then, in a post function on the "Create" transition on my worflow, I added :

Add Post Function.PNG

and put the full path to the script:

Post Function.PNG

Don't forget to change execution order so that the script runs before the creation of the issue.

Hope this help !

How do i change execution order? Excuse my ignorance i am new to this.

Just move your mouse to the right of the line, up and down arrows will show up:

UpDownArrows.PNG

PS : don't forget to vote for my answer...

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

713 views 6 13
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you