Update the custom date field when a last commented By Assignee
Asked by Lakshmi S:

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

}
```


Answers:
author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Lakshmi S](/t5/user/viewprofilepage/user-id/11952)

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](https://community.atlassian.com/t5/image/serverpage/image-id/346996iB0B23FD79FFBC8BB/image-size/large?v=v2&px=999 "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

comments: author: Lakshmi S
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

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.


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Lakshmi S](/t5/user/viewprofilepage/user-id/11952)

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


author: Lakshmi S
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

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.


author: Ram Kumar Aravindakshan _Adaptavist_
body: 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](https://community.atlassian.com/t5/image/serverpage/image-id/347524i3105628FE1AB5D7F/image-size/large?v=v2&px=999 "behaviour_config.png")

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

Thank you and Kind regards,  
Ram


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Lakshmi S](/t5/user/viewprofilepage/user-id/11952)

Has your question been answered?

If yes, please accept the answer.

Thank you and Kind regards,  
Ram


author: Tomasz Kowalczyk
body: Hi [@Lakshmi S](/t5/user/viewprofilepage/user-id/11952) , 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

comments: author: Tomasz Kowalczyk
body: i mean issue updated event not transitioned which does not exist


Tags: data-center

jira-data-center

scriptrunner