You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
I would like to add 1 month to a custom date field (Next Cycle Date) and then update that date field upon a workflow transition (post-function).
I have the current script but it's not doing anything, not sure what I am doing wrong.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.UpdateIssueRequest
import java.text.SimpleDateFormat
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MMM/yy");
java.sql.Timestamp nextCycleDate = (java.sql.Timestamp)issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_10603")) //get current "next cycle date"
nextCycleDate.setMonth(nextCycleDate.getMonth() + 1) //add 1 month to current cycle date to get next cycle date
issue.setCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_10603"), nextCycleDate) //update next cycle date
**Please read through replies as well below**
I was trying many different things to get to the solution. After countless hours spent on testing and encountering many different errors, here is the correct code.
import com.atlassian.jira.component.ComponentAccessor
import java.sql.Date
import java.sql.Timestamp
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import org.apache.log4j.Logger
import org.apache.log4j.Level
// Manager
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
//Values
CustomField currentDate = customFieldManager.getCustomFieldObject("customfield_10603")
Timestamp currentDateValue = (Timestamp)issue.getCustomFieldValue(currentDate)
Date newDateValue = new Date(currentDateValue.getTime())
//Add 1 month
newDateValue.setMonth(currentDateValue.getMonth() + 1)
//Update custom field
issue.setCustomFieldValue(currentDate, newDateValue.toTimestamp())
There were many different issues I encountered via debugging (major thanks to Nic as I would not have even started with the logging if he had not mentioned it).
Thank you
Could you have a look in the logs for errors? Also this script must be first in the list of the post functions for the transition. And this script will not work for the create issue transition.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Alexey,
Script is first in the list, workflow transition is not on create issue.
Here's the error I found..
2018-03-24 18:14:47,977 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: RAR-5, actionId: 11, file: <inline script> java.lang.NullPointerException: Cannot invoke method getMonth() on null object at Script101.run(Script101.groovy:12)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That tells you that the object you are trying to use .getMonth on does not exist. Which in turn means the call to populate it has returned nothing, so have a look at the like that is supposed to set nextCycleDate. It's getting nothing from the issue.getCustomFieldValue call. So it's either faulty code or the issue does not have the field set to a value.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I can definitely see that the field is set within the issue, and I can understand how it could be faulty code.. I'm pretty new to scriptrunner and Java so I do not understand it as well as someone else would.
I have also doublechecked to make sure customfield 10603 is the field that I am trying to get the value from (aka the field that has the value in it and is not blank).
Possibly something is wrong with my code here? I'm not sure.
java.sql.Timestamp nextCycleDate = (java.sql.Timestamp)issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_10603"))
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yeah, it's not that easy to read, let alone write! Your code actually looks broadly right, and it's not throwing errors, so you must be doing it right here.
I'd head for a debugger in the IDE, but if you're not sure of that, the simplistic brute-force way to find it is to break down the multiple calls and log where it fails.
So:
This will error somewhere, and that should tell you which bit is going wrong
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Make sure that the id (10603) of the custom field is right.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Alexey,
10603 is correct, I have triple-checked.
Nic,
I'm having a bit of trouble with the logging method. Please see two screenshots showing errors.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Fixed the logging problem by importing the logging package and changing log to log.debug throughout.
The three log debug lines are as follows:
2018-03-26 17:06:01,719 DEBUG [acme.CreateSubtask]: Next Cycle Date 2018-03-26 17:06:01,719 DEBUG [acme.CreateSubtask]: 2017-01-15 00:00:00.0 2018-03-26 17:06:01,719 DEBUG [acme.CreateSubtask]: 2017-01-15 00:00:00.0
Not getting any errors, so I'm not sure what I'm doing wrong here and why it isn't working.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Do you mean, that there is no NullPointerException anymore? If so, then where did you attach the post function? Is it the create transition? Is it last of first in the list of postfunctions?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The NullPointerException error came only one time.. I'm not sure why that error occurred.
Now it is working fine, and it is in the first of the postfunctions, and the transition is not create.. The transition is in the middle of the workflow.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Try like this and put the post function last in the list of post functions.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.UpdateIssueRequest
import java.text.SimpleDateFormat
import com.atlassian.jira.event.type.EventDispatchOption
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MMM/yy");
java.sql.Timestamp nextCycleDate = (java.sql.Timestamp)issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_10603")) //get current "next cycle date"
nextCycleDate.setMonth(nextCycleDate.getMonth() + 1) //add 1 month to current cycle date to get next cycle date
issue.setCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_10603"), nextCycleDate)
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Did that, and the custom field still does not change. Tried it as last in the postfunction list and first, but no difference. No errors found in the scriptrunner logs either.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Are you sure that the post function is executed?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I added this code:
def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)
log.debug "foo bar"
Then I re-ran the workflow transition to check if foo bar was logged in the scriptrunner logs, which would ensure that the workflow postfunction executed, and it did:
2018-03-28 09:39:36,714 DEBUG [acme.CreateSubtask]: foo bar
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have made some modifications, but I would appreciate the community's help here.
I am using the following code:
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField csDate1 = customFieldManager.getCustomFieldObject("customfield_10603")
Timestamp csDate1Value = (Timestamp)getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_10603"))
Date csNewDateValue = new Date(csDate1Value.getMonth() + 1)
issue.setCustomFieldValue(csDate1, csNewDateValue)
Now I am getting an error as follows:
2018-03-30 14:21:36,648 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: RAR-4, actionId: 11, file: <inline script> java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object at Script229.run(Script229.groovy:27)
I am not sure how it sees it as null.. I go to http://myjirainstance:8080/rest/api/latest/issue/RAR-4 and have the value there:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am not sure, how you call the script. But it seems to me that there is a error. You call getCustomFieldValue, but you should call issue.getCustomFieldValue.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I was trying many different things to get to the solution. After countless hours spent on testing and encountering many different errors, here is the correct code.
import com.atlassian.jira.component.ComponentAccessor
import java.sql.Date
import java.sql.Timestamp
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import org.apache.log4j.Logger
import org.apache.log4j.Level
// Manager
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
//Values
CustomField currentDate = customFieldManager.getCustomFieldObject("customfield_10603")
Timestamp currentDateValue = (Timestamp)issue.getCustomFieldValue(currentDate)
Date newDateValue = new Date(currentDateValue.getTime())
//Add 1 month
newDateValue.setMonth(currentDateValue.getMonth() + 1)
//Update custom field
issue.setCustomFieldValue(currentDate, newDateValue.toTimestamp())
There were many different issues I encountered via debugging (major thanks to Nic as I would not have even started with the logging if he had not mentioned it).
Thank you
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Just sharing a full script. If you want to add further condition based on certain fields, certain months are added to the date field (in my case, its based on priority field)
import com.atlassian.jira.component.ComponentAccessor import java.sql.Timestamp def focusIssue = issue def customFieldManager = ComponentAccessor.getCustomFieldManager() def issueStartDate = customFieldManager.getCustomFieldObjectsByName("Issue Start Date").first()?:null def priority = issue.getPriority()?:null Timestamp currentDateValue = (Timestamp)issue.getCustomFieldValue(issueStartDate)?:null Date maxDate = new Date(currentDateValue.getTime())?:null if (priority.name == 'Low'){ maxDate.setMonth(currentDateValue.getMonth() + 24)} else if (priority.name == 'Medium'){ maxDate.setMonth(currentDateValue.getMonth() + 24) } else if (priority.name == 'High'){ maxDate.setMonth(currentDateValue.getMonth() + 12) } return maxDate
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.