Table format Values needs to auto comment on specific custom filed value in specific transition.

Pramod Kumar Reddy July 26, 2023

Hello Team,

Based on Issue Type - “Bug” and Custom Field - “SubDelivery” (Select list) Specific Value , Auto comment needs to be updated.

Please Find Comment Template that needs to be updated in comment section. PFA

When we tried to update comment in table format in post function, it was not working. kindly help us to resolve this issue.

This code is currently under investigation by my side. You may make suggestions or contribute alternative code.

Script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue

// Replace 'CUSTOM_FIELD_ID' with the ID of your custom field "SubDelivery"
def customFieldName = 'Sub-Delivery'

// Create the table format comment
def tableComment = """
|| Heading 1 || Heading 2 || Heading 3 ||
| Value 1 | Value 2 | Value 3 |
| Value A | Value B | Value C |
"""

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def commentManager = ComponentAccessor.getCommentManager()

// Get the issue that triggered the event
Issue eventIssue = event.issue

def customField = customFieldManager.getCustomFieldObject(customFieldName)

if (eventIssue && customField) {
// Get the current value of the custom field "SubDelivery"
def subDeliveryValue = eventIssue.getCustomFieldValue(customField)

// Check if the value of the custom field "SubDelivery" is "ABC"
if (subDeliveryValue == "ABC") {
// Add the table format comment
commentManager.create(eventIssue, event.user, tableComment, true)

// Log message (optional)
log.info("Table format comment added for issue ${eventIssue.key}.")
} else {
// Log message (optional)
log.info("No action needed for issue ${eventIssue.key}.")
}
} else {
log.error("Issue or custom field not found.")
}

But we are getting “Issue or custom field not found” in logs

 

 

 

 

 

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 26, 2023

Hi @Pramod Kumar Reddy

Your requirement is pretty simple. It would be best to use ScriptRunner HAPI feature to simplify the code.

With HAPI, all you need to do is this:-

def issue = event.issue

def sampleListValue = issue.getCustomFieldValue('Sample List').toString()

def commentValue = """
|| Heading 1 || Heading 2 || Heading 3 ||
| Value 1 | Value 2 | Value 3 |
| Value A | Value B | Value C |
"""

if (issue.issueType.name == 'Bug' && sampleListValue == 'Option 1') {
issue.addComment(commentValue)
}

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

Below is a screenshot of the Listener configuration:-

listener_config.png

If you observe, the Listener is configured with 2 events, i.e. Issue Created and Issue Updated. Hence, when the Issue is created or updated, the Issue Type is Bug, and the option selected from the List is Option 1, the Comment will be added.

In regards to your current code, from a high-level view, I can already see that there is an error, i.e.:-

def customField = customFieldManager.getCustomFieldObject(customFieldName)

The correct way to declare this if you are doing it via field name is:

def customField = customFieldManager.getCustomFieldObjectsByName(customFieldName).first()

This is why I would suggest using my approach via HAPI where you do not need to make any such declaration.

Below are a couple of test screenshots for your reference:-

For the first test, I will test the Issue Created event.

1. First, I am creating a new Bug and setting the Sample List to Option 1, as shown in the screenshot below:-

test1.png

2. As expected, once the Issue is created, the Comment is also added, as shown in the screenshot below:-

test2.png

 

For the next test, I will test the Issue Updated Event.

 

1. First, I am going to create a new Bug, and I am not going to select any value from the Sample List as shown in the screenshot below:-test3.png

2. As expected, the Issue is created, and the Comment is not included, as shown in the screenshot below:-

test4.png

3. Next, I edit the Issue and  select Option 1 from the Sample List as shown in the screenshot below:-

test5.png

4. As expected, the Comment is added as shown in the screenshot below:-

test6.png

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

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer