Forums

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

Post function Set a Date by default

Olivier MAZEN
Contributor
September 11, 2018

Hello there,

 

i am facing an issue with a post function.

 

When i want to close an issue, i get a transition screen.

 

In the screen i get a cloture date (custom field). I want to set this field by default with the date of the current day.

 

To reach my goal, i set a post function in my transition where i define my transition screen.

 

The post function triggered a custom script (describe below).

 

When i use the transition:

i) the field isn't set with the date of today

ii) when i want to save and move on to a close the issue i get an error according to the JAVA date library.

 

I dont get it, I use another similare script in the same WF and it's works well (triggered by postfunction too)

 

Here is my script:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import java.util.Date.*;


def Issue issue = issue

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

def cfdatedefin = customFieldManager.getCustomFieldObject("customfield_10204")

Date Today = new Date()

cfdatedefin.setFormValue(Today.toString())

 

 

Do you have any information to help me on this point?

6 answers

1 accepted

0 votes
Answer accepted
Olivier MAZEN
Contributor
October 11, 2018

Hello there,

 

i finaly reach my goal.

 

I add a initialiser in a behavior.

 

I test my transitionname and if it is ok i set a date of today in my formfield.

 

Hope it's help someone.

Petter Gonçalves
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 11, 2018

Thank you for sharing it, Olivier!

Have a nice day.

2 votes
Petter Gonçalves
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 11, 2018

Hello Olivier,

Actually, you don't need to use a Script to add the current date to your custom field. 

You can use the post-function Update issue Custom Field and set the custom field value with the macro '%%CURRENT_DATETIME%%' or '%%CURRENT_DATE%%', which will be replaced by the date/time or only the date when you execute of the transition.

Please, let me know if this option helps.

Olivier MAZEN
Contributor
September 12, 2018

Hello Petterson,

 

Unfortunatly i didn"t have this post function. All i get is describ in the following picture.post function.png

Philemon Nkafu
Contributor
August 2, 2019

Hello Petter,

I used the above macro re '%%CURRENT_DATE%%' and am getting an "..illegal workflow operation." transition error when attempt the transition. 

Like Sakthiya Giri likes this
Petter Gonçalves
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 2, 2019

Hello @Philemon Nkafu

Can you please double-check if the field you are trying to set is a Date field instead of a Date and Time field?

If it is a Date and Time field, try to use '%%CURRENT_DATETIME%%'.

Philemon Nkafu
Contributor
August 2, 2019

The field I created is a simple date picker field. 

Philemon Nkafu
Contributor
August 5, 2019

Hello @Petter Gonçalves 

I checked the CF and can reconfirm that it is a date picker field. Am working on Jira server v7.4. Could that be the problem i.e does the solution work only for cloud?

Tharun Shankararaman
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 18, 2020

Thank you Petter, this works beautifully well

Like Petter Gonçalves likes this
0 votes
Olivier MAZEN
Contributor
September 25, 2018

Hello Petterson,

 

Thanks for your reply!

At least my script is theoricly correct. I have performed a last check this morning and there is no behavior, no another post function, nothing than can get into conflict with my code.

As you said , i gonna ask the Adaptavist support.

 

Thanks for you time !

Petter Gonçalves
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 25, 2018

You are welcome, Olivier.

Please, let us know the feedback of Adaptavist.

0 votes
Olivier MAZEN
Contributor
September 21, 2018

Hello There,

 

i was thinking about something, does it is possible to set a default value in a transition screen ?

 

Olivier 

Petter Gonçalves
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 21, 2018

Hello Olivier,

I'm sorry to hear that you are facing difficulties to apply this functionality. In fact, the Script provided should be working for you.

About your last question, you can only set a Default value for a field in the creation screen of the issue, but not in the posterior transition screens.

There's a discussion about it here: Default value on custom field for a transition screen

Another option would be to use a system field (Due date, for example) instead of a custom field, so you would be able to use the post function Update Issue field.

If you would like to further troubleshoot your Groovy Script, I think that the Adaptavist support will be able to help you with this.

0 votes
Olivier MAZEN
Contributor
September 17, 2018

Hello,

 

No one have any suggestion for setting a Date customfield with the date of today threw a post function script?

With only Scriptrunner ?

 

Olivier

Petter Gonçalves
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 17, 2018

Hello Olivier,

Using ScriptRunner, you can try the solution mentioned by Vasiliy Zverev on the answer below:

How do I set a custom field with the current date/time in a post function?

Olivier MAZEN
Contributor
September 20, 2018

Hello Petterson,

 

based on your answer, i try this custom script added to a post funciton:

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import java.util.Date.*;
// get current issue
Issue issue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()

// get today's date
Date today = new java.sql.Timestamp(new Date().getTime())


CustomField cfdatedefin = customFieldManager.getCustomFieldObject("customfield_10204")
issue.setCustomFieldValue(cfdatedefin, today)

 

and it is not working.

 

This is realy anoying to didn't get a simple behavior that pre default a custom field with a simple date.

I have to paid an add on for trying to do this, and finaly it is not possible or so ?

 

The more i try, the more i search the less i reach my point.

 

Atlassian really have to develop this basic functionnality in anative JIRA / JIRA SD imo.

 

Do you have any suggestion where i miss up ?

0 votes
Petter Gonçalves
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 12, 2018

Hello Olivier,

Sorry, I didn't notice that you are using JIRA Server. Here's the plugin that provides the mentioned post function for JIRA Server: 

JIRA Suite Utilities

Olivier MAZEN
Contributor
September 12, 2018

Helloo,

 

thank for your feedback.

 

unfortunatly i can't get this add on (politic in firm about add-on is very clear) i have to handle it only with script runner ....

 

Olivier

Suggest an answer

Log in or Sign up to answer