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

I want to compare custom date field and today

jun lee January 25, 2023

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

0 votes
Ashley Hudson
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.
January 26, 2023

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
AUG Leaders

Atlassian Community Events