Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,300,418
Community Members
 
Community Events
165
Community Groups

Updating the Date field (adding days) depending on the value in the "text" field - Scrypt runner

Edited

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.

1 comment

Tuncay Senturk Community Leader Sep 03, 2020

Hi @Pawel Podl 

Welcome to the community!

Even your request needs to be clarified, I will try to write some Groovy code which might boost your development.

Below code is just a snippet, I haven't tested it though.

Assuming;

- cfDate is the date field that you want to update according to the value in another field.

- cfMonths is the field which stores the value of how many moths will be added to the date

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParameters

def cfDate = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_abc") // date custom field
def cfMonths = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_xyz") // months custom field

def monthsToAdd = issue.getCustomFieldValue(cfMonths) as int
if (issue.type.name.equals("Test Report")) {
def date = Calendar.getInstance()
date.add(Calendar.MONTH, monthsToAdd)
issue.setCustomFieldValue(cfDate, dateAdd)
}
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

 

I hope it helps

Tuncay

Like Pawel Podl likes this

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.

I got a few bugs thet I can't deal with.

class.PNGmore.PNGundec.PNG

Tuncay Senturk Community Leader Sep 04, 2020

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
}
Like Pawel Podl likes this
Tuncay Senturk Community Leader Sep 04, 2020

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()
Like Pawel Podl likes this

Thanks @Tuncay Senturk 

Thank you.
I only have a problem with the option in the screen. I don't know what class this is.

class1.PNG

Tuncay Senturk Community Leader Sep 04, 2020

Please add this one 

import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
Like Pawel Podl likes this

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)

 

undeccc.PNG

I probably started scrypt too complicated for the beginning; (

Yhank You So Much For Your Help.

and dateAdd is undeclared.

undecccc.PNG

Tuncay Senturk Community Leader Sep 04, 2020

That should be date

Like Pawel Podl likes this

@Tuncay Senturk would you help me with this mistake?

 

undeccc.PNG

I threw the code above.

Thank You So Much For Your Help.

Tuncay Senturk Community Leader Sep 04, 2020

cfield should be changed to the variable you defined for date field. I can't see in this picture but, it might be cfDate.

Like Pawel Podl likes this

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)

Tuncay Senturk Community Leader Sep 07, 2020

It seems that cfMonths (customfield_12018) is not a number custom field, it is the date field. I think you confused the customfields.

Like Pawel Podl likes this

Correct, field is of type Select List (single choice). Can I get help with type changes? description on the screen.

pola.PNG

Tuncay Senturk Community Leader Sep 07, 2020

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

Like Pawel Podl likes this

Thank you very much for your help. An error popped up

error.PNG

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 

Tuncay Senturk Community Leader Sep 07, 2020

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())
Like Pawel Podl likes this

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)
Tuncay Senturk Community Leader Sep 07, 2020

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)
Tuncay Senturk Community Leader Sep 07, 2020

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 oneerror 01.PNG

Tuncay Senturk Community Leader Sep 07, 2020

 

import java.sql.Timestamp
Like Pawel Podl likes this

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)

Like Tuncay Senturk likes this

eventually :D 

Please do not forget mark this answer as accepted so that the users with similar problem would see the solution.

Cheers

Like Pawel Podl likes this

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)

 

1.PNG2.PNG3.PNG4.PNG

Comment

Log in or Sign up to comment
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you