How to add current user name and date(with time) to read only text field in jira cloud?

Danish Hussain Sabunwala June 2, 2021

Hello Team,

I have a select list(single choice) custom field and once I enter the value in, I need to add the name of the user who updated the field along with the time updated?

I am working on JIRA cloud and wanted know if in any way it is possible or not?

Thanks and regards,

Danish Hussain Sabunwala

2 answers

1 accepted

2 votes
Answer accepted
Callum Carlile _Automation Consultants_
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 2, 2021

Hi @Danish Hussain Sabunwala 

You can achieve this using an automation rule. The trigger would be when the field value is changed for your single-select list.

Your action would be the Edit Issues action, where you would select the text field in which you want to add in the details. If you used a smart value of {{initiator.displayName}}, {{now.jqlDateTime}}, then this would fill the field with User's display name, 2021-06-02 15:15.

This will work if the text field is on the view screen but not on the edit screen, so will remain a read-only field

Danish Hussain Sabunwala June 2, 2021

Hello @Callum Carlile _Automation Consultants_ ,
Thanks a lot for the response.

Maybe my question confused you, sorry for that. 

I get that the following, if applied, would only update these values one's and if I try to update it a second time, my first values will be lost.

Example: My single-select list contains : A1 and A2 values. If I select A1 the read-only text field should have "User's display name, 2021-06-02 15:15: now If another user comes and selects A2; Then the read-only text field should have

"User's display name, 2021-06-02 15:15:

Another User's display name, 2021-06-02 16:16:.

Is there any workaround for this?

Thanks and regards,

Danish Hussain 

Callum Carlile _Automation Consultants_
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 3, 2021

Hi @Danish Hussain Sabunwala  Yeah this is possible :) if you use the smart value of:

{{issue.name_of_text_field}} : {{initiator.displayName}}, {{now.jqlDateTime}}

The first smart value here just uses whatever's currently in the field, then adds the name and date/time of the initiator to it.

So if a user edits the single-select list the first time:

Name1, date1/time1

...then another user changes this field value

Name1, date1/time1 : Name2, date2/time2

And so on...

You can of course edit the smart value to use any sort of commas, colons or whatever you like to separate the values, I have just used these as an example, the important part is the stuff within the curly brackets

Danish Hussain Sabunwala June 4, 2021

Hello @Callum Carlile _Automation Consultants_ ,  this is exactly what I was looking for.

Thanks and Regards,

Danish Hussain

0 votes
Max Lim _Adaptavist_
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.
June 3, 2021

This information is actually in the issue's history. Anyway, you can achieve this using ScriptRunner's Script Listeners.

To do that:

1. Navigate ScriptRunner > Script Listeners > Add Listener > make appropriate configurations

2. Under "On these events", choose "Issue Updated" or any events suitable for your use case.

3. Under "Code to run", paste in following snippet:

def result = get("/rest/api/2/issue/${issue.key}/changelog")
.header('Content-Type', 'application/json')
.asObject(Map)

def changes = result.body.values.findAll({
it.items.field == ["Change type"] //Replace with your field name
})

if (!changes.isEmpty()) {
def lastUpdatedBy = changes.last().author.displayName as String
def lastUpdatedTime = Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSSZ", changes.last().created).format("yy-MM-dd HH:mm", TimeZone.getTimeZone("Asia/Kuala_Lumpur"))

// get custom fields
def lastUpdatedByCf = get("/rest/api/2/field")
.asObject(List)
.body
.find {(it as Map).name == 'Last Updated'} as Map //Replace with your read-only text field name

def resp = put("/rest/api/2/issue/${issue.key}")
.header('Content-Type', 'application/json')
.body([
fields: [
(lastUpdatedByCf.id): lastUpdatedBy + ", " + lastUpdatedTime,
]
])
.asString()
}

4. You need to manually set the time zone because you are displaying string and Jira will not adjust automatically to the user's time zone and will always display time in UTC. To check all available time zones., you can run following snippet in ScriptRunner > Script Console:

TimeZone.getAvailableIDs()

 

An alternative would be implement this using Scripted Fields. The script should be similar.

Max Lim _Adaptavist_
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.
June 4, 2021

An update to the script, which is more efficient:

def hasFieldChanged = changelog.items.any { it.field == "Change type" } //Replace with your monitoring field name

if (hasFieldChanged) {
def fullChangelog = get("/rest/api/2/issue/${issue.key}/changelog")
.header('Content-Type', 'application/json')
.asObject(Map)
.body
.values
.last()

def lastUpdatedBy = fullChangelog.author.displayName as String
def lastUpdatedTime = Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSSZ", fullChangelog.created).format("yy-MM-dd HH:mm", TimeZone.getTimeZone("Asia/Kuala_Lumpur"))

def lastUpdatedByCf = get("/rest/api/2/field")
.asObject(List)
.body
.find {(it as Map).name == 'Last Updated'} as Map //Replace with your read-only text field name

def resp = put("/rest/api/2/issue/${issue.key}")
.header('Content-Type', 'application/json')
.body([
fields: [
(lastUpdatedByCf.id): lastUpdatedBy + ", " + lastUpdatedTime,
]
])
.asString()
}  

Suggest an answer

Log in or Sign up to answer