Scriptrunner. How to enter a value of a custom field into the field "Comments"?

Leo Epstein February 21, 2022

Hello,

If the custom field "Due Date" is not empty, the date from this field should be entered into the field "Comments" with some text like this:

MyText: DueDateValue

Which code can I use to do that?

Many thanks in advance.

Regards

Leo

1 answer

1 accepted

0 votes
Answer accepted
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 21, 2022

Hi @Leo Epstein do you need to insert the code during transition or right on the screen before the transition is executed?

Leo Epstein February 21, 2022

Hi @Martin Bayer _MoroSystems_ s_r_o__ ,

I need the code for a Post Function in the Create Issue Transaction.

Regards,

Leo

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 22, 2022

Hi @Leo Epstein , cool. Adaptavist has a library of code snippets. You can go for insipration there.

The code needed to create new comment is here: https://library.adaptavist.com/entity/add-comment-to-an-issue-in-a-post-function

You also need to load Due Date value (https://docs.atlassian.com/software/jira/docs/api/8.22.0/com/atlassian/jira/issue/Issue.html#getDueDate--), you can do it using:

def dueDateValue = issue.getDueDate()

dueDateValue is of Timestamp type here, so you need to convert it to String:

So in sum it should be something like:

def dueDateValue = issue.getDueDate()
if(dueDateValue){
def dueDateString = new SimpleDateFormat("MM/dd/yyyy").format(dueDateValue)

// the body of the comment
final String commentBody = """MyText: ${dueDateString}"""

// the author of the comment will be the logged in user
def author = ComponentAccessor.jiraAuthenticationContext.loggedInUser

ComponentAccessor.commentManager.create(issue, author, commentBody, false)

}
Like Leo Epstein likes this
Leo Epstein February 22, 2022

Hi @Martin Bayer _MoroSystems_ s_r_o__ ,

Thanks a lot for your kind suggestions.

I cannot see the name of my field "Due Date" in your code.

Will that work anyway?

Regards

Leo

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 22, 2022

If you mean Due Date (system field) it will work using code 

issue.getDueDate()

If you defined custom field for Due Date, we need to update the script, so let me know :) 

Like Leo Epstein likes this
Leo Epstein February 22, 2022

Hi @Martin Bayer _MoroSystems_ s_r_o__ ,

Sorry, I meant the custom field "Due Date".

To say exactly its real name is "Wunschtermin". :)

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 22, 2022

Ok, so you can again get some inspiration in library by Adaptavist. You can read custom field's value like this:

def DUEDATE_CF_ID = 11111L // you need to replace it with your field's ID
def dueDateCF = ComponentAccessor.customFieldManager.getCustomFieldObject(DUEDATE_CF_ID)
def dueDateCFValue = issue.getCustomFieldValue(dueDateCF)

IN sum it could be something like:

 

def DUEDATE_CF_ID = 11111L // you need to replace it with your field's ID
def dueDateCF = ComponentAccessor.customFieldManager.getCustomFieldObject(DUEDATE_CF_ID)
def dueDateCFValue = issue.getCustomFieldValue(dueDateCF)

if(dueDateCFValue){
def dueDateCFString = new SimpleDateFormat("MM/dd/yyyy").format(dueDateCFValue)

// the body of the comment
final String commentBody = """MyText: ${dueDateCFString}"""

// the author of the comment will be the logged in user
def author = ComponentAccessor.jiraAuthenticationContext.loggedInUser

ComponentAccessor.commentManager.create(issue, author, commentBody, false)

}
Like Leo Epstein likes this
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 22, 2022

Let me know if it works for you :)

Like Leo Epstein likes this
Leo Epstein February 22, 2022

Hi @Martin Bayer _MoroSystems_ s_r_o__ ,

Thanks again!

I will try your code and let you know then.

I will also read the the library you kindly suggested.

Just one general question:

Is the getting the value of custom field's per field ID is easier than per filed name?

Or is it only possible?

Or there is no differences between both ways?

Regards, Leo

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 22, 2022

There are both options, but you can have more custom fields with the same name and the only one custom field with given ID. If you need to get the custom field using its name, and you know there is always only one field with given name, you cen get collection of custom fields by name and select the first one:

https://docs.atlassian.com/software/jira/docs/api/8.22.0/com/atlassian/jira/issue/CustomFieldManager.html#getCustomFieldObjectsByName-java.lang.String-

Like Leo Epstein likes this
Leo Epstein February 22, 2022

Thank you!

Leo Epstein February 24, 2022

@Martin Bayer _MoroSystems_ s_r_o__ 

Hi Martin,

What should I add?

"unable to resolve class SimpleDateFormat".

unable.jpg

Regards

Leo

Leo Epstein February 24, 2022

OK, I found and added these...

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.security.JiraAuthenticationContext;
import java.text.SimpleDateFormat

Leo Epstein February 24, 2022

Hi @Martin Bayer _MoroSystems_ s_r_o__ ,

I hope that was correct, was it?

import java.text.SimpleDateFormat

Anyway, the code works now, at least when I create a new issue in Jira on the server from my laptop.

Thank you very much!

Regards

Leo

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 24, 2022

Hi @Leo Epstein yes, you are correct :) I simplified the code snippet. Good job :)

Like Leo Epstein likes this
Leo Epstein February 25, 2022

Hi @Martin Bayer _MoroSystems_ s_r_o__ ,

Thanks again!

Just one more question... ;)
The comment ist added with the name of the current user. This is very good.

Is it anyway possible, to have there something other instead?

Like a NON existing account "Jira System" or some other EXISTING account or without the name at all?

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 1, 2022

Hi @Leo Epstein , sorry for late response... you can do it. You need to set author variable to other user (it can't be non existing user). You can use for example this method https://docs.atlassian.com/software/jira/docs/api/8.22.0/com/atlassian/jira/user/util/UserManager.html#getUserByName-java.lang.String- to find user's instance by it's name.

Like Leo Epstein likes this
Leo Epstein March 14, 2022

Hi @Martin Bayer _MoroSystems_ s_r_o__ ,
Thank you very much!

Regards
Leo

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events