I am trying to create a post-function in a "create issue" transition, and it is not working as expected. I am trying to use field values provided by users on the "create issue" screen to automatically set field values of the issue itself. However, my post function has no effect. I have read that the order of events in which the post function takes place matters, and I have tried moving the post function to all combinations of before and after the "Creates the issue originally" and "Re-index an issue to keep indexes in sync with the database." stages.
In some positions, I am able to access the user-provided field values but can't change them. In other positions, I am able to change the fields but I can't access the user-provided values. There does not appear to be a solution/location where I am able to *both* read the user-provided values *and* change them according to the script.
I have read the documentation about post-functions, for example this page:
https://docs.adaptavist.com/sr4js/latest/features/workflows/post-functions/custom-post-functions/custom-post-function-examples/set-issue-attributes
On this page, it mentions the importance of running your post-function "before the standard function" and if that doesn't work you need to "use a more complex method." But I am unable to find any information on this "more complex method"
Any assistance is appreciated (including pointing me in the direction of where I can learn more about this "more complex method")
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger
import org.apache.log4j.Level
import java.util.regex.*;
log.setLevel(Level.DEBUG)
def components = ComponentAccessor.projectComponentManager.findComponentsByIssue(issue)
log.info(components)
if(components != null){
def count = 0
//Get the list of component provided during the create screen
//Test look up their descriptions to see if they match some condition
def pattern = Pattern.compile("some-regex-here", Pattern.CASE_INSENSITIVE)
for(def component in components){
def compDesc = component.getDescription()
log.info(compDesc)
if(!compDesc) continue;
if(pattern.matcher(compDesc).find()){
count++
}
}
log.info("Match Count: " + count)
//Set a custom user field if the above component check matches
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10400")
if(count > 0){
log.info("Component match")
def user = ComponentAccessor.getUserManager().getUserByKey("myUser")
issue.setCustomFieldValue(cf, user)
}
}
Hi @Jordan Paul ,
probelm is that you are not updating your issue.
At the end of your code (when you set the custom field value), you should use the following method https://docs.atlassian.com/software/jira/docs/api/8.2.0/com/atlassian/jira/issue/IssueManager.html#updateIssue-com.atlassian.jira.user.ApplicationUser-com.atlassian.jira.issue.MutableIssue-com.atlassian.jira.event.type.EventDispatchOption-boolean-
...
IssueManager issueManager = ComponentAccesso.getIssueManager();
ApplicationUser loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
....
if(count > 0){
log.info("Component match")
def user = ComponentAccessor.getUserManager().getUserByKey("myUser")
MutableIssue mIssue = (MutableIssue)issue;
mIssue.setCustomFieldValue(cf, user);
issueManager.updateIssue(loggedInUser,mIssue,EventDispatchOption.DO_NOT_DISPATCH, false)
}
Above an example of missing code.
Hope this helps,
Fabio
Thanks for your reply @Fabio Racobaldo _Catworkx_ ! I continued to plug away at it on my own and what I managed to come up with (and what is working now) is this. It looks fundamentally similar to your suggestion
def issueService = ComponentAccessor.issueService
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
...
if(count > 0){
def issueInputParams = new IssueInputParametersImpl()
issueInputParams.addCustomFieldValue("customfield_10400", "myUser")
def updateValidationResult = issueService.validateUpdate(loggedInUser, issue.id, issueInputParams)
def issueUpdateResult = issueService.update(loggedInUser, updateValidationResult, EventDispatchOption.ISSUE_UPDATED, false /*sendMail*/)
}
Perhaps the MutableIssue type in conjunction with the issueManager's updateIssue method is doing basically the same thing as working with IssueInputParametersImpl and issueService's update method? Is there a reason to prefer one approach over the other?
One slight hiccup: after issue create and on the recently created issue's history tab, I see there is an extra row showing that the logged in user has made changes to set the custom field to "myUser". I suppose this is not entirely unexpected, but is there anyway to avoid this? Is that why you have set EventDispatchOption.DO_NOT_DISPATCH ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Jordan Paul ,
IssueService is more flexible because it allows to validate result before executing the update. Btw, you can use IssueService or IssueManager at your convenience.
The update operation will enter a new history item in the issue history and you can't avoid it.
I set EventDispatchOption.DO_NOT_DISPATCH because, otherwise, an issue edit event will be fired. If you have notification for creation and update, Jira will send two separate email.
Hope this clarifies your doubts,
Fabio
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.