Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Insight Groovyscript Date Calculation

This is more of an inquiry --can this be possible-- and so I don't expect a solution, but would like to know if I'm wasting my efforts.

I am very new to groovyscript-- very new to scripting in general. However, we utilize Mindville Insight with our Jira Database and a customer has requested the capability to add a date from an attribute field of an object to a number (given in days/years) from another attribute field and update a 3rd attribute field with the new date.

The goal is to track life cycles for equipment, etc. 
(Equipment A was fielded on 1 JAN 21 -- has a life cycle of 3 years --  End of Life will be 1 JAN 24)

The customer also wants the End of Life date to update if the life cycle is changed (in case they find a given equipment only lasts 2 years with heavy use, etc.

I'm assuming this would run via Insight automation once the object is created/edited.
As a bonus harder question, the customer wants to know if this script could call an attribute from a different object in this script... for example. Model A has a life cycle of 3 years, Equipments B, C, D are the same Model A, but have different fielded date attributes-- the End of Life field for each would pull that life cycle field from the model object. (And again, if the life cycle field is updated, then the end of life field on each item would update accordingly...

I've found this code for adding dates and a number, but am unsure how to "call" the attribute fields.

"Groovy also has a Time Category class which gives you DSL style syntax for manipulating dates. Here is an example:

import org.codehaus.groovy.runtime.TimeCategory
now = new Date()
println now
use(TimeCategory) {
footballPractice = now + 1.week - 4.days + 2.hours - 3.seconds
println footballPractice
which will produce output like this:

Sat Sep 27 10:00:12 EST 2008
Tue Sep 30 12:00:09 EST 2008


Thank you for any insight (pun intended) and help in this matter.
If you know of any previous help requests that reflect my query, please let me know, as I haven't had much luck finding a similar question for Insight Groovy scripts, just groovyscripts or jira service desk scripts.

1 answer

0 votes

The short answer is generally... if you can think of it and you can identify all the decision points, you can probably do it using a script.

Using the utility class I shared in this article ... you could write a script like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.util.JiraHome
def jiraHome = ComponentAccessor.getComponentOfType(JiraHome.class).home
Class InsightUtils = new GroovyClassLoader(getClass().getClassLoader()).parseClass(new File("$jiraHome/path/to/scripts/InsightUtils.groovy"))

//object is already defined when running from insight automation with object created/object updated events
//This assumes the equipment object has a "Model" attribute that is a reference to another object type that has "Life Cycle Days" attribute with a number of days
def lifeCycleDays = InsighUtils.getObjectAttributeFromDotNotation(object, 'Model.Life Cycle Days')
//if your model object has n years as lifecycle, we'd have to do it like this:
def lifeCycleYears = InsighUtils.getObjectAttributeFromDotNotation(object, 'Model.Life Cycle Years')
def endOfLifeDate
def dateAttributeValue = InsightUtils.getObjectAttributeFromDotNotation(object, 'Some Date Attribute')
endOfLifeDate = use(TimeCategory) { dateAttributeValue + lifeCycleDays.days }
} else if(lifeCycleYears) {
endOfLifeDate = use(TimeCategory) { dateAttributeValue + lifeCycleYears.years }

InsighUtils.setObjectAttribute(object, 'End of Life Date', endOfLifeDate)

Now, this just shows the high-level logic ... you'd have to either install the InsightUtils class somewhere in your jira home directory or go extract the code you need from it. The real magic is in "getObjectAttributeFromDotNotation" and "setObjectAttribute" methods. 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Apps & Integrations

Marketplace Partner Spotlight: Fine Software

Happy New Year, everybody! What better way to kick off 2022 than with a new Marketplace Partner Spotlight ✨ This month, we're getting to know Daniel Franz, co-founder of Fine Software. Fine Software ...

417 views 2 15
Read article

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