how can i populate a custom field with users local current date?

Hi,

Globally we are using JIRA and need to record when something is marked as done it stores the local date on the users machine not the date on the server for that issue.

How can i achieve this? i know it will need to be coded but im not sure on how to achieve this, i do have script runner.

2 answers

0 votes
Vasiliy Zverev Community Champion Mar 15, 2016

So, you need to perform two steps:

1) learn how to update custom field of date type

2) learn how to get local type.

Lets start with step 1:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField

import java.sql.Timestamp

CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("custom field name 1")
if(customField == null)
    return "no such custom field"

//Not requared into a postfunction
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("ZVER-10")
if(issue == null)
    return "an issue not found"

//Here we set requred date
Calendar dateToSet = Calendar.getInstance(); //here you could get local time on user machine. But I am not sure

issue.setCustomFieldValue(customField, new Timestamp(dateToSet.getTimeInMillis()))

//Here we do the update. Not requared into postfunction
ComponentAccessor.getIssueManager().updateIssue(ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser(), issue, EventDispatchOption.ISSUE_UPDATED, false)

I think that this code should set local time, but I am not sure. Try it.

where do i put all this code into exactly? sorry im only used to putting java/javascript into the description of a field

 

Vasiliy Zverev Community Champion Mar 16, 2016

This code should be inserted into Scripted postfunction provided by ScriptRunner Plugin.

For test properties you can use ScriptConsole. See this:https://answers.atlassian.com/questions/32982259

In this case you should specify an issue (see line MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("ZVER-10"))

Do i delete all bits of code that have //not required into post function ?

So like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
 
import java.sql.Timestamp
 
CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("customfield_11520")
if(customField == null)
    return "no such custom field"
 
 
//Here we set requred date
Calendar dateToSet = Calendar.getInstance(); //here you could get local time on user machine. But I am not sure
 
issue.setCustomFieldValue(customField, new Timestamp(dateToSet.getTimeInMillis()))

Also do i just use a normal date picker custom field? or do i need to use a scripted field or calculated date/time field? 

I managed to get it working however it does not pull the current users local machine date, i believe it is pulling the server date as i am in australia so it should of set the value to 22/03/2016 however it set the date to 21/03/2016

Vasiliy Zverev Community Champion Mar 21, 2016

Try to check date/time on your server.

yeah its pulling the sever local date i believe, i need it to pull the current users actual machine time.. or thinking about it do i need to incorporate the users current timezone profile at all?

Vasiliy Zverev Community Champion Mar 22, 2016

If you use this code into a servlet you could try to send current time from local machine using javascript. This could by stored as a param into URL. 

For examle: ......?curtime=08:32am

"when something is marked as done it stores the local date on the users machine not the date on the server for that issue"

That's not the case... it stores the server date but will display the date in the timezone of whoever is looking at it. At least that's since JIRA 4.4.

If you want to get the user timezone, you will need to look up the timezone preference for the user and make the adjustment.


Thats the problem........ for reporting purposes i dont want to change my timezone to each different destination every time i have to do reports.. we have over 8 timezones across various projects... i need to calculate from this date to this date regardless of timezone thus having a field that stores the local machine time a way to solve this problem

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Monday in Confluence

Organizing your space just got easier - Page Tree Drag & Drop is here

Hi Community! I’m Elaine, Confluence Product Manager. You may have read my earlier post about page tree in space navigation sidebar. I'm excited to share another improvement that helps you organize ...

67 views 3 2
Join discussion

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