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.
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
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.