Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

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

Chris Ghazanchian
Contributor
March 5, 2018

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
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 5, 2018

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

Chris Ghazanchian
Contributor
March 8, 2018

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).

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 8, 2018

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)
Chris Ghazanchian
Contributor
March 9, 2018

Thank you for your response, I truly appreciate it!

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

scriptrunnererror.PNG

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 9, 2018
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())
Chris Ghazanchian
Contributor
March 10, 2018

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)
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 10, 2018

What is your Jira version?

Chris Ghazanchian
Contributor
March 10, 2018

7.6.2

Chris Ghazanchian
Contributor
March 10, 2018

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.

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 10, 2018
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))
German Rodriguez Bolanos October 10, 2018

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

David Willson December 2, 2021

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

Suggest an answer

Log in or Sign up to answer