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,362,237
Community Members
 
Community Events
168
Community Groups

Post-Function ScriptRunner script to update summary with name of following month

Edited

Hello! I am new to scriptrunner and programming in general so I am struggling with how to achieve what I would like.

Here's my scenario-

I have issues within my project that have a custom date field "Billing Cycle Date".

I would like to run a workflow post function script which will take the billing cycle date, calculate what the next month is, and then append the name of the month into the summary.

So, let's say for a given issue:

Summary: Issue1

Billing Cycle Date: 3/5/2018

Upon workflow transition, postfunction script runs and sets Summary to "Issue1 - April"

 

I have spent multiple days trying to figure this out but nothing is working properly, any guidance would be greatly appreciated!

1 answer

1 accepted

2 votes
Answer accepted

Could you provide one of your scripts, which is not working?

I figured this much out, which worked.

 

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

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
String field1Value = (String)issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Cycle Date"))

issue.setSummary(field1Value)

 

But now, I need it to add exactly one month to the date (not 30 days), and then convert the month to the actual name and set it in the summary.

Not sure if that's possible, I'm exploring other options (combining multiple automation tasks or multiple fields).

Like this

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

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
java.sql.Timestamp field1Value = (java.sql.Timestamp)issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Cycle Date"))

field1Value.setMonth(field1Value.getMonth() + 1)

issue.setSummary(field1Value)

Thank you for your response, I truly appreciate it!

I am getting the following error, and am unsure what it means.

scriptrunnererror.PNG

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

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
java.sql.Timestamp field1Value = (java.sql.Timestamp)issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Cycle Date"))

field1Value.setMonth(field1Value.getMonth() + 1)

issue.setSummary(field1Value.toString())

This is the error I got after using that script.

2018-03-10 11:03:38,576 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-03-10 11:03:38,577 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: RAR-4, actionId: 41, file: <inline script>
java.lang.NullPointerException
 at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
 at com.atlassian.jira.issue.Issue$getCustomFieldValue$6.call(Unknown Source)
 at Script119.run(Script119.groovy:7)

I modified the script to use getCustomFieldObject (with the customfield's id) instead of getCustomFieldObjectByName, and then got this error.

2018-03-10 11:13:02,566 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-03-10 11:13:02,581 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: RAR-4, actionId: 41, file: <inline script>
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'August 24, 2018' with class 'java.lang.String' to class 'java.sql.Timestamp'
 at Script125.run(Script125.groovy:7)

What is your Jira version?

My mistake, I apologize. That script ended up working, the field I was using it on was just a text field (I forgot I had tried a few other things before implementing the new script).

 

Now it is working great except it is putting the whole date and time into the summary. I would only like to put the name of the month.. for example if the date is June 1st, 2018, then only put "July"(next month) in the summary. Is that possible? I'm trying to research it myself right now.

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

SimpleDateFormat monthFormatter = new SimpleDateFormat("MMMM");
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
java.sql.Timestamp field1Value = (java.sql.Timestamp)issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("CycleDate"))
field1Value.setMonth(field1Value.getMonth() + 1)
issue.setSummary(monthFormatter.format(field1Value))

Hi have the same issue:
image.png

any ideas? basically I need to set a value for the Description, but I don't now which command do i need for this, thanks

Did you ever find out? I am looking to do the same.

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events