Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,456,480
Community Members
 
Community Events
176
Community Groups

I want to compare custom date field and today

I want to compare custom date field and today

 

Below script was succeeded.

-----------

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.user.util.UserUtil
import java.util.Date.*
import utils.*

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def workpercent = customFieldManager.getCustomFieldObject("customfield_13500")

CustomField finishDateField = customFieldManager.getCustomFieldObject("customfield_10230"); //finish date
long finishDate = (issue.getCustomFieldValue(finishDateField) as Date).getTime(); //finish date value

def today = (new Date() as Date).getTime() //current datetime


if(finishDate > today) {
issue.setCustomFieldValue(workpercent, 100 as double)
} else {
return true
}

------------

 

When I use grater script, it doesn't work. 

if(finishDate > today) --> if(finishDate >= today)

 

So I tested like below, but failed. 

if(finishDate.equals(today))

if(finishDate.isequal(today))

if(finishDate.compareTo(today) == 0)

 

How can I fix?

 

Thanks in advance.

1 answer

Hey @jun lee ,

I've found this script that might just do the trick for you! It essentially compares the due date field to the resolved date field but you can simply just replace the resolved date field with your custom field value! 

Here is the script that I found:
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.apache.log4j.Category
import java.text.SimpleDateFormat

// Logging variables
Category infoLog = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
infoLog.setLevel(org.apache.log4j.Level.INFO)

// Constants declarations
def targetMetFieldName = "Target Met"
Long targetMetYesOptionID = 10001
Long targetMetNoOptionID = 10002

// We grab the related fields here (put in your custom field value)
ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
CustomField targetMetField = customFieldManager.getCustomFieldObjectByName(targetMetFieldName)
String dueDateValue = issue.getDueDate()
String resolvedDateValue = issue.getResolutionDate()

// Some error checking
if (dueDateValue.equals(null)) {
    infoLog.info "No due date specified for issue ${issue.getKey}"
} else if (resolvedDateValue.equals(null)) {
    infoLog.info "No resolved date specified for issue ${issue.getKey}"
} else if (targetMetField.equals(null)) {
    infoLog.info "Custom field ${targetMetFieldName} does not exist in the instance."
}

// Parsing the Resolved Date and Due Date fields as a Date() object
Date dueDate = new SimpleDateFormat("yyyy-MM-dd").parse(dueDateValue)
Date resolveDate = new SimpleDateFormat("yyyy-MM-dd").parse(resolvedDateValue)

infoLog.info "Due Date for ${issue.getKey()} is ${dueDate.toString()}"
infoLog.info "Resolved Date for ${issue.getKey()} is ${resolveDate.toString()}"

// We grab the available options from the custom field
Object selectValue = issue.getCustomFieldValue(targetMetField) //grab current value for the custom field
FieldLayoutItem fieldLayoutItem = ComponentManager.getInstance().getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem(targetMetField)
OptionsManager optionsManager = (OptionsManager) ComponentManager.getComponentInstanceOfType(OptionsManager.class)
Option targetMetNoOption = optionsManager.findByOptionId(targetMetNoOptionID)
Option targetMetYesOption = optionsManager.findByOptionId(targetMetYesOptionID)

if (targetMetNoOption.equals(null)) {
    infoLog.info "There is no option with the ID ${targetMetNoOptionID}"
} else if (targetMetYesOption.equals(null)) {
    infoLog.info "There is no option with the ID ${targetMetYesOptionID}"
}

// Date comparisons go here
if (resolveDate.after(dueDate)) {
    targetMetField.updateValue(fieldLayoutItem, issue, new ModifiedValue(selectValue, targetMetNoOption), new DefaultIssueChangeHolder())
    infoLog.info "Custom field ${targetMetField.getFieldName()} for issue ${issue.getKey()} is set to No"

} else {
    targetMetField.updateValue(fieldLayoutItem, issue, new ModifiedValue(selectValue, targetMetYesOption), new DefaultIssueChangeHolder())
    infoLog.info "Custom field ${targetMetField.getFieldName()} for issue ${issue.getKey()} is set to Yes"
}

Let me know if this works / if you have any issues / questions! :) This simply returns a true or false outcome like the solution above! 

Kind Regards,

Ashley Hudson 

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events