How to compare current day with my custom field maintenance date (dtMWDATE)?

Hi,

I'm trying to compare the maintenance date (dtMWDATE), that is my custom field, with the current day. So what I'm trying to do is everytime that user select a data for maintanece (dtMWDATE) and this date is out dated, the script should fill this  custom field value with current date

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.util.ErrorCollection
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

Class SubTask = new GroovyClassLoader(getClass().getClassLoader()).parseClass(new File("./scripts/tools/SubTask.groovy"));

def myissue = "SDP-8908"
IssueManager issueManagerManual = ComponentAccessor.getIssueManager()
Issue issue = issueManagerManual.getIssueObject("$myissue")

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
ErrorCollection errCollection;

def strStartDate = customFieldManager.getCustomFieldObjectByName('Start Date')
def dtStartDate = issue.getCustomFieldValue(strStartDate)

def strMWDate = customFieldManager.getCustomFieldObjectByName('MW Date')
def dtMWDate = issue.getCustomFieldValue(strMWDate)

CustomField omCf = customFieldManager.getCustomFieldObjectByName('O&M Assignee')
def om = issue.getCustomFieldValue(omCf)

String userOm = null

if (om == null){
userOm = null
} else {
userOm = om.name
}

def subTask1 = SubTask.createSubTask(issue, "Prepare Testbed", "SPB", "O&M task", ((dtMWDate-7 <= date)? date: dtMWDate-7), dtStartDate, null, userOm)


def subTask6 = SubTask.createSubTask(issue, "Execute Maintenance Window", "SPB", "MW task", dtMWDate, dtStartDate, dtMWDate, userOm, "MW")
//SubTask.setLabel(subTask6, "MW")

I'm did a little search and I found this:

dtMWDate-7 <= date)? date: dtMWDate-7

Is it right? Or do i missing some declaration on date?

 

Thank you!

1 answer

This widget could not be displayed.

You can get the current date like this:

Date date = new Date()

And then you can cast your custom field values to a Date like this:

def dtStartDate = issue.getCustomFieldValue(strStartDate) as Date

Your script should look something like this, but I haven't tested it:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.util.ErrorCollection
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

Class SubTask = new GroovyClassLoader(getClass().getClassLoader()).parseClass(new File("./scripts/tools/SubTask.groovy"));

def myissue = "SDP-8908"
IssueManager issueManagerManual = ComponentAccessor.getIssueManager()
Issue issue = issueManagerManual.getIssueObject("$myissue")

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
ErrorCollection errCollection;

def strStartDate = customFieldManager.getCustomFieldObjectByName('Start Date')
def dtStartDate = issue.getCustomFieldValue(strStartDate) as Date

def strMWDate = customFieldManager.getCustomFieldObjectByName('MW Date')
def dtMWDate = issue.getCustomFieldValue(strMWDate) as Date

CustomField omCf = customFieldManager.getCustomFieldObjectByName('O&M Assignee')
def om = issue.getCustomFieldValue(omCf)

String userOm = null

if (om == null){
userOm = null
} else {
userOm = om.name
}

Date date = new Date()
def subTask1 = SubTask.createSubTask(issue, "Prepare Testbed", "SPB", "O&M task", ((dtMWDate-7 <= date)? date: dtMWDate-7), dtStartDate, null, userOm)

def subTask6 = SubTask.createSubTask(issue, "Execute Maintenance Window", "SPB", "MW task", dtMWDate, dtStartDate, dtMWDate, userOm, "MW")
//SubTask.setLabel(subTask6, "MW")

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted yesterday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

71 views 1 0
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you