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
Hi @Leo Epstein do you need to insert the code during transition or right on the screen before the transition is executed?
Hi @Martin Bayer _MoroSystems_ s_r_o__ ,
I need the code for a Post Function in the Create Issue Transaction.
Regards,
Leo
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Martin Bayer _MoroSystems_ s_r_o__ ,
Sorry, I meant the custom field "Due Date".
To say exactly its real name is "Wunschtermin". :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Let me know if it works for you :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Martin Bayer _MoroSystems_ s_r_o__
Hi Martin,
What should I add?
"unable to resolve class SimpleDateFormat".
Regards
Leo
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Leo Epstein yes, you are correct :) I simplified the code snippet. Good job :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.