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.
Updating the Date field (adding days) depending on the value in the "text" field - Scrypt runner
Hello, I am a beginner in programming. I need to write a script that will check if the task type is, for example: "test report" if so, it will set the "xxx" field (date time field) depending on the value in the "yyy" field (text field).
If "yyy" is 1 month, then "xxx" is set to + 30 days from the date in the "xxx" field.
if "yyy" is 3 months, then "xxx" is set + 3 months from the date that was previously in this field.
Can I count on help? I will be very grateful.
Thank you very much for your time and help @Tuncay Senturk
If the field is a Picklist (single selection) with the values "miesiąc, kwartał, połrocze, rok", will customfield be able to handle this type of field?
Thank You very much for help.
You have to put below code before using issueManager
def issueManager = ComponentAccessor.getIssueManager()
For select list value, the code below can help
def cFieldValue = issue.getCustomFieldValue(cField)
def selectedValue = ((LazyLoadedOption)cFieldValue).getValue()
if (selectedValue.equals("miesiąc")) {
// do anything here
}
Also add below import statement to the beginning
import com.atlassian.jira.event.type.EventDispatchOption
You have to populate the user field accordingly. It changes where you are using this code; post function, behavior, ..
To get logged in user, you can use below
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
Thanks @Tuncay Senturk
Thank you.
I only have a problem with the option in the screen. I don't know what class this is.
Please add this one
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
Thank you very much @Tuncay Senturk
there is still something wrong with LazyLoadeOption, can you help?
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
def cfDate = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12019") // date custom field
def cfMonths = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12018") // months custom field
def issueManager = ComponentAccessor.getIssueManager()
def cFieldValue = issue.getCustomFieldValue(cField)
def selectedValue = ((LazyLoadedOption)cFieldValue).getValue ()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser ()
def monthsToAdd = issue.getCustomFieldValue(cfMonths) as int
if (selectedValue.equals ("miesiąc")){
def date = Calendar.getInstance()
date.add(Calendar.MONTH, monthsToAdd)
issue.setCustomFieldValue(cfDate, dateAdd)
}
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
I probably started scrypt too complicated for the beginning; (
Yhank You So Much For Your Help.
That should be date
@Tuncay Senturk would you help me with this mistake?
I threw the code above.
Thank You So Much For Your Help.
cfield should be changed to the variable you defined for date field. I can't see in this picture but, it might be cfDate.
Hello, I have a problem with an error, can I count on help?
------CODE-----
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
def cfDate = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12019") // date custom field
def cfMonths = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12018") // months custom field
def issueManager = ComponentAccessor.getIssueManager()
def cFieldValue = issue.getCustomFieldValue(cfDate)
def selectedValue = ((LazyLoadedOption)cFieldValue).getValue ()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser ()
def monthsToAdd = issue.getCustomFieldValue(cfMonths) as int
if (selectedValue.equals ("miesięczna")){
def date = Calendar.getInstance()
date.add(Calendar.MONTH, monthsToAdd)
issue.setCustomFieldValue(cfDate, date )
}
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
-----Error-----
2020-09-07 08:24:55,706 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2020-09-07 08:24:55,706 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: FUKO2-1690, actionId: 221, file: <inline script>
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '2020-09-04 00:00:00.0' with class 'java.sql.Timestamp' to class 'int'
at Script6.run(Script6.groovy:13)
It seems that cfMonths (customfield_12018) is not a number custom field, it is the date field. I think you confused the customfields.
Correct, field is of type Select List (single choice). Can I get help with type changes? description on the screen.
Hi,
I just guessed your scenario and wrote below code.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
def issueManager = ComponentAccessor.getIssueManager()
def cfDate = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12018") // date custom field
def cfCyklic = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12019") // select custom field
def dateFieldValue = issue.getCustomFieldValue(cfDate)
def cfCyklicValue = issue.getCustomFieldValue(cfCyklic)
def selectedOption = ((LazyLoadedOption)cfCyklicValue).getValue ()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser ()
def date = Calendar.getInstance()
if (selectedOption.equals ("miesięczna")){
// if miesięczna selected add 1 month
date.add(Calendar.MONTH, 1)
} else if (selectedOption.equals ("kwartalna")){
// if kwartalna selected add 2 month
date.add(Calendar.MONTH, 2)
}
issue.setCustomFieldValue(cfDate, date )
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
you can add logs accordingly to see the fields' values for debugging purposes.
Tuncay
Thank you very much for your help. An error popped up
Time (on server): Mon Sep 07 2020 11:18:44 GMT+0200 (czas środkowoeuropejski letni)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2020-09-07 11:18:44,168 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2020-09-07 11:18:44,168 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: FUKO2-1690, actionId: 221, file: <inline script> java.lang.ClassCastException: java.util.GregorianCalendar cannot be cast to java.util.Date at com.atlassian.jira.issue.customfields.impl.DateCFType.getDbValueFromObject(DateCFType.java:57) at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.updateValue(AbstractSingleFieldType.java:151) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:426) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396) at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:704) at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:669) at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:655) at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:214) at com.atlassian.jira.issue.IssueManager$updateIssue.call(Unknown Source) at Script35.run(Script35.groovy:25)
An error popped up
Yeah, I think it is because we are trying to set customfield value with Calendar rather than date value. Simply change with this one.
issue.setCustomFieldValue(cfDate, date.toInstant())
After swapping I still have a problem :(
-----------CODE---------
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
def issueManager = ComponentAccessor.getIssueManager()
def cfDate = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12018") // date custom field
def cfCyklic = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12019") // select custom field
def dateFieldValue = issue.getCustomFieldValue(cfDate)
def cfCyklicValue = issue.getCustomFieldValue(cfCyklic)
def selectedOption = ((LazyLoadedOption)cfCyklicValue).getValue ()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser ()
def date = Calendar.getInstance()
if (selectedOption.equals ("miesięczna")){
// if miesięczna selected add 1 month
date.add(Calendar.MONTH, 1)
} else if (selectedOption.equals ("kwartalna")){
// if kwartalna selected add 2 month
date.add(Calendar.MONTH, 2)
}
issue.setCustomFieldValue(cfDate, date.toInstant())
//issue.setCustomFieldValue(cfDate, date )
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
------ERRROR-----
Time (on server): Mon Sep 07 2020 12:03:33 GMT+0200 (czas środkowoeuropejski letni)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2020-09-07 12:03:33,866 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2020-09-07 12:03:33,882 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: FUKO2-1690, actionId: 221, file: <inline script> java.lang.ClassCastException: java.time.Instant cannot be cast to java.util.Date at com.atlassian.jira.issue.customfields.impl.DateCFType.getDbValueFromObject(DateCFType.java:57) at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.updateValue(AbstractSingleFieldType.java:151) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:426) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396) at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:704) at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:669) at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:655) at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:214) at com.atlassian.jira.issue.IssueManager$updateIssue.call(Unknown Source) at Script39.run(Script39.groovy:25)
whoops sorry, how about this one?
issue.setCustomFieldValue(cfDate, new Date(date.getTime()))
or this one
issue.setCustomFieldValue(cfDate, Date.from(date.toInstant()))
I used
issue.setCustomFieldValue(cfDate, Date.from(date.toInstant()))
and have error...
----code-----
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
def issueManager = ComponentAccessor.getIssueManager()
def cfDate = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12018") // date custom field
def cfCyklic = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12019") // select custom field
def dateFieldValue = issue.getCustomFieldValue(cfDate)
def cfCyklicValue = issue.getCustomFieldValue(cfCyklic)
def selectedOption = ((LazyLoadedOption)cfCyklicValue).getValue ()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser ()
def date = Calendar.getInstance()
if (selectedOption.equals ("miesięczna")){
// if miesięczna selected add 1 month
date.add(Calendar.MONTH, 1)
} else if (selectedOption.equals ("kwartalna")){
// if kwartalna selected add 2 month
date.add(Calendar.MONTH, 2)
}
issue.setCustomFieldValue(cfDate, Date.from(date.toInstant()))
issue.setCustomFieldValue(cfDate, new Date(date.getTime()))
//issue.setCustomFieldValue(cfDate, date.toInstant())
//issue.setCustomFieldValue(cfDate, date )
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
----ERORR----
Time (on server): Mon Sep 07 2020 13:21:28 GMT+0200 (czas środkowoeuropejski letni)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2020-09-07 13:21:28,632 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2020-09-07 13:21:28,632 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: FUKO2-1690, actionId: 221, file: <inline script> java.lang.IllegalArgumentException: Java type java.util.Date not currently supported. Sorry. at org.ofbiz.core.entity.jdbc.SqlJdbcUtil.getFieldType(SqlJdbcUtil.java:893) at org.ofbiz.core.entity.jdbc.SqlJdbcUtil.setValue(SqlJdbcUtil.java:784) at org.ofbiz.core.entity.jdbc.SqlJdbcUtil.setValue(SqlJdbcUtil.java:753) at org.ofbiz.core.entity.jdbc.SqlJdbcUtil.setValues(SqlJdbcUtil.java:549) at org.ofbiz.core.entity.GenericDAO.singleInsert(GenericDAO.java:197) at org.ofbiz.core.entity.GenericDAO.insert(GenericDAO.java:171) at org.ofbiz.core.entity.GenericHelperDAO.create(GenericHelperDAO.java:89) at org.ofbiz.core.entity.GenericDelegator.create(GenericDelegator.java:562) at org.ofbiz.core.entity.GenericDelegator.create(GenericDelegator.java:548) at org.ofbiz.core.entity.GenericValue.create(GenericValue.java:114) at com.atlassian.jira.ofbiz.DefaultOfBizDelegator.createValue(DefaultOfBizDelegator.java:296) at com.atlassian.jira.ofbiz.WrappingOfBizDelegator.createValue(WrappingOfBizDelegator.java:172) at com.atlassian.jira.issue.customfields.persistence.OfBizCustomFieldValuePersister.createValuesInt(OfBizCustomFieldValuePersister.java:129) at com.atlassian.jira.issue.customfields.persistence.OfBizCustomFieldValuePersister.updateValues(OfBizCustomFieldValuePersister.java:139) at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.updateValues(EagerLoadingOfBizCustomFieldPersister.java:48) at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.updateValue(AbstractSingleFieldType.java:151) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:426) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396) at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:704) at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:669) at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:655) at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:214) at com.atlassian.jira.issue.IssueManager$updateIssue.call(Unknown Source) at Script46.run(Script46.groovy:27)
Actually, this is getting tricky, you are supposed to know a bit programming and my codes would be supposed to help you get deeper :)
Anyway, I think we are close. So, as far as I understand customfields are only accepting Timestamp, so how about trying this one?
issue.setCustomFieldValue(cfDate, new Timestamp(date.getTimeInMillis()))
As I wrote before, I must have gone too far, I should start with simpler scripts, but we are already so far.
I got a class error when adding the last one
import java.sql.Timestamp
It works :) I don't know how to thank you. The last time I took on such a scrypt ... Let me start with smaller, simple scripts. I am very grateful for your help.
For users, I provide a code that works. Best regards @Tuncay Senturk and thank you very much again.
---CODE---
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import java.sql.Timestamp
def issueManager = ComponentAccessor.getIssueManager()
def cfDate = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12018") // date custom field
def cfCyklic = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12019") // select custom field
def dateFieldValue = issue.getCustomFieldValue(cfDate)
def cfCyklicValue = issue.getCustomFieldValue(cfCyklic)
def selectedOption = ((LazyLoadedOption)cfCyklicValue).getValue ()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser ()
def date = Calendar.getInstance()
if (selectedOption.equals ("miesięczna")){
// if miesięczna selected add 1 month
date.add(Calendar.MONTH, 1)
} else if (selectedOption.equals ("kwartalna")){
// if kwartalna selected add 2 month
date.add(Calendar.MONTH, 2)
}
issue.setCustomFieldValue(cfDate, new Timestamp(date.getTimeInMillis()))
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
eventually :D
Please do not forget mark this answer as accepted so that the users with similar problem would see the solution.
Cheers
Hi, @Tuncay Senturk I have a problem, why is the scrypt that works on postfunction not working in automation for JIRA?
----CODE----
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import java.sql.Timestamp
def issueManager = ComponentAccessor.getIssueManager()
def cfDate = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12018") // date custom field
def cfCyklic = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12019") // select custom field
def dateFieldValue = issue.getCustomFieldValue(cfDate)
def cfCyklicValue = issue.getCustomFieldValue(cfCyklic)
def selectedOption = ((LazyLoadedOption)cfCyklicValue).getValue ()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser ()
def date = Calendar.getInstance()
if (selectedOption.equals ("miesięczna")){
// if miesięczna selected add 1 month
date.add(Calendar.MONTH, 1)
} else if (selectedOption.equals ("kwartalna")){
// if kwartalna selected add 2 month
date.add(Calendar.MONTH, 3)
} else if (selectedOption.equals ("półroczna")){
// if kwartalna selected add 2 month
date.add(Calendar.MONTH, 6)
} else if (selectedOption.equals ("roczna")){
// if kwartalna selected add 2 month
date.add(Calendar.MONTH, 12)
}
issue.setCustomFieldValue(cfDate, new Timestamp(date.getTimeInMillis()))
//sue.setCustomFieldValue(cfDate, Date.from(date.toInstant()))
//issue.setCustomFieldValue(cfDate, new Date(date.getTime()))
//issue.setCustomFieldValue(cfDate, date.toInstant())
//issue.setCustomFieldValue(cfDate, date )
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)