Forums

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

Scriptrunner not able to get last comment into string and perform comparision

newstart Newstart May 17, 2021

Hi all,

I am trying to get the last comment and check if that contains a string. If string found transition the issue.

Trying to do this via scriptrunner. Have created the below script and added it to the listener. But am getting into the "null" pointer exception.


Interestingly, when am returning "comment.body" this is working fine but when storing in a string, it is not getting stored and not able to perform any string operations on it.

Example last comment on JIRA ticket:  Ram has updated the status of a ticket linked to this issue from [Open] to [Resolved]  


Based on this comment ticket will need to be moved to Resolved (transition id :11)

Your quick help is much appreciated.

 

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.component.ComponentAccessor


def issueService = ComponentAccessor.getIssueService()
IssueManager im = ComponentAccessor.getIssueManager();
MutableIssue issue = im.getIssueObject("ITP-26")
def commentManager = ComponentAccessor.getCommentManager()
def comment = commentManager.getLastComment (issue)

//return comment.body is printing the last comment properly
// if i use println(comment.body) - getting null value.

String lastcomment = comment.getBody()
// this is the last comment on the ticket: Ram has updated the status of a ticket linked to this issue from [Open] to [Resolved]

lastcomment.contains("has updated the status of a ticket linked to this issue from [Open] to [Resolved]")
// is returning false

// when trying to put in if condition getting null
// if(lastcomment.contains("has updated the status of a ticket linked to this issue from [Open] to [Resolved]"))

 

 



Regards,
Ram

1 answer

0 votes
Ravi Sagar _Sparxsys_
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.
May 17, 2021

Hi @newstart Newstart 

What exactly is here in the null pointer exception error, maybe paste your error as well.

Null pointer exception means that you are trying to call a method on a null object. So let us say your issue has no comment then you cannot call comment.body hence the error.

Add some error handling in your code to check those cases.

Ravi

newstart Newstart May 17, 2021

Hi Ravi,

Thanks for your quick response.

def comment = commentManager.getLastComment (issue)

return comment.body  --- this is returning last comment.

However, when trying to store the last comment in a string and perform string operations it is not working

Screenshot 2021-05-17 at 5.24.29 PM.png

 

String lastcomment = comment.body
// this is the last comment on the ticket: Ram has updated the status of a ticket linked to this issue from [Open] to [Resolved]
println(lastcomment)
if (lastcomment.contains("has updated the status of a ticket linked to this issue from [Open] to [Resolved]"))
println("Matching Found")

else
println("Matching Not Found")

 

 

Screenshot 2021-05-17 at 5.27.23 PM.png

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events