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

t March 15, 2016

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
JamieA
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.
March 15, 2016

"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.


t March 15, 2016

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

0 votes
Vasiliy Zverev
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.
March 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.

t March 16, 2016

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
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.
March 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"))

t March 21, 2016

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? 

t March 21, 2016

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
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.
March 21, 2016

Try to check date/time on your server.

t March 22, 2016

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
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.
March 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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events