Forums

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

Update the custom date field when a last commented By Assignee

Lakshmi CH
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.
September 10, 2024

Hi Team,

We have a scriptrunner listener to capture the date field when a last commented by assignee, the field date is updating when the ticket assignee is adding the comment, and same date and time adding to the "Assignee Last Comment Date" field , but its not updating when we are adding the comment to the ticket transitioning from one status to another status, is there anything we need add to the script or any oder was missed in this listener ?

 

 

 

import java.text.SimpleDateFormat

def issue = event.issue

def simpleDateFormat = new SimpleDateFormat('dd/MMM/yy h:mm a')

if (issue.comments && issue.comments.last() && issue.assignee) { // Check for null values

  def lastComment = issue.comments.last()

  if (lastComment.authorApplicationUser == issue.assignee) {

    try {

      issue.update {

        setCustomFieldValue('Assignee Last Comment Date', simpleDateFormat.format(lastComment.updated)) // Replace with custom field ID or name

      }

      log.info("Assignee Last Comment Date updated successfully.") // Informative log message

    } catch (Exception e) {

      log.error("Error updating custom field: ${e.message}") // Log errors

    }

  }

} else {

  log.debug("No comments or assignee found.") // Debug log for missing data

}

 

2 answers

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
September 12, 2024 edited

Hi @Lakshmi CH

For your requirement, you must use ScriptRunner's Custom Listener along with the Issue Commented event.

Below is a sample working code for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat

def issue = event.issue

def commentManager = ComponentAccessor.commentManager
def comments = commentManager.getComments(issue)
def dateFormat = new SimpleDateFormat('dd/MMM/yy h:mm a')

issue.update {
comments.each { comment ->
if (comment.authorApplicationUser == issue.assignee) {
setCustomFieldValue('Latest Commented By Assignee', dateFormat.format(comment.created))
}
}
}

Please note that the sample working code above is not 100% exact to your environment. Hence, you must make the required modifications.

Below is a screenshot of the Listener configuration:-

listener_config.png

What this code does is that it reads through all the comment histories of an issue and will only extract the latest comment that the assignee of the Issue has added. From there, it will update the Date Time file with the Date and Timestamp when the Issue was updated.

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

 

Lakshmi CH
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.
September 12, 2024

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

The problem is :  

The system updates correctly when a comment is added directly to the ticket. However, when the ticket is transitioned from one status to another and a comment is added using the comment field on the transition screen, the 'Assignee Last Comment Date' field is not updated, even when the ticket is assigned to the user transitioning the ticket.

NOTE : I already added "Issue Commented" event to the listener. 

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
September 13, 2024

Hi @Lakshmi CH

Please clarify: When exactly do you want to get the time the issue was updated via the Transition screen? Is it when the comment is added in the Transition screen or after the transition has been completed?

If it's the former, you will need to use ScriptRunner's Behaviour. If it's the latter, then the Post-Function would be the best approach.

I am looking forward to your feedback and clarification.

Thank you and Kind regards,
Ram

Lakshmi CH
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.
September 13, 2024

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

I need to update the field "Assignee Last Comment Date"

* When the assignee of the ticket directing commenting on the ticket
AND
* When the assignee of the ticket transitions the ticket, and adds the comment on the transition screen

In both cases, it needs to be updated.

 

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
September 16, 2024

Hi Lakshmi,

If you intend to update the Assignee Last Comment Date field when the issue is being transitioned, then in addition to the Listener code, you must include a Server-Side Behaviour code for the Comment field. This will update the Assign Last Comment Date field once the comment is added by the issue's assignee on the transition screen.

Below is a sample working Server-Side Behaviour code for the comment field:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import java.text.SimpleDateFormat

@BaseScript FieldBehaviours behaviours
def comment = getFieldById(fieldChanged)
def commentValue = comment.value
def latestComment = getFieldByName('Latest Commented By Assignee')

def dateFormat = new SimpleDateFormat('dd/MMM/yy h:mm a')

if (commentValue && underlyingIssue && actionName == 'In Progress') {
underlyingIssue.comments.each {
if (it.authorApplicationUser == underlyingIssue.assignee) {
latestComment.setFormValue(dateFormat.format(it.created))
}
}
}

Please note that the sample working code above is not 100% exact to your environment. Hence, you must make the required modifications.

Below is a screenshot of the Server-Side Behaviour configuration for your reference:-

behaviour_config.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

 

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
September 17, 2024

Hi @Lakshmi CH

Has your question been answered?

If yes, please accept the answer.

Thank you and Kind regards,
Ram

0 votes
Tomasz Kowalczyk
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.
September 11, 2024

Hi @Lakshmi CH , your listener probably is listening on issue commented event. When youre adding a comment there is no issue commented event firing but issue triansition event. That's probably why you're not getting it done on transition. You can add issue transition event to the listener and it should work 

Tomasz Kowalczyk
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.
September 11, 2024

i mean issue updated event not transitioned which does not exist

Suggest an answer

Log in or Sign up to answer