Validator for checking the filling in of a field of the date time type

Artem Semenov June 30, 2021

Hi guys!

I'm trying to make a validator on the transition to checking a field of the date-time type. When filling it out, if the date and time are less than the current working day, a warning is displayed. If the field is empty or the next business day is specified, then perform the transition.

I tried this option:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import java.sql.Timestamp

def duedatetime = customFieldManager.getCustomFieldObject("customfield_11417")
def noticket = new Timestamp((new Date() +1).getTime())

if ((duedatetime > noticket) || duedate == null)
return true

 

but it does not work with a custom field of the date-time type.

Maybe someone has faced a similar problem, and knows how to solve it correctly?

I would be grateful for your help!

 

 

 

2 answers

0 votes
Artem Semenov July 1, 2021

@Nic Brough -Adaptavist- hello!

I can also ask you a question, in continuation of this topic. I'm trying to add another condition to this script, but the script checks only the first one, can you tell me with the syntax?

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import java.sql.Timestamp


def ContecstNoSLA = customFieldManager.getCustomFieldObject("customfield_13801")
def cFieldValue = issue.getCustomFieldValue(ContecstNoSLA)

def duedatetimeCf = customFieldManager.getCustomFieldObject("customfield_11420")
def duedatetime = issue.getCustomFieldValue(duedatetimeCf)

def noticket = new Timestamp((new Date() +1).getTime())
def noticket2 = new Timestamp((new Date() +2).getTime())

if (((cFieldValue?.value == "Yes") || (cFieldValue?.value == "No")) && ((duedatetime > noticket) || duedatetime == null))
return true

else (((cFieldValue?.value == null") || (cFieldValue?.value == "Maybe")) && ((duedatetime > noticket2) || duedatetime == null))
return true

 

Sorry if this is a stupid question, I'm just starting to figure it out :-)

I will be grateful for your help!

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 1, 2021

I'm not the strongest on various languages, probably because I've worked with so many (The phrase "Jack of all trades, master of none" probably could apply as "Nic of many languages, master of only one (which you've never heard of)"). 

I've said that because I am not sure your if/then/else structure is doing what you think in the last two paragraphs of your code, and would want to check it carefully.  But assuming that there is nothing wrong with that, the next place to look is at the values in your custom fields.

In your code, cFieldValue takes the value for the current issue for customfield_13801, but I can't tell what that value is.  Not the actual value, but the type of data it is.  With the other one, the duedatetime, your code assumes it is a date/time stamp, and because your code works, we know it is.  But we don't know about the other one!  

My guess would be that it is one of the two single-select field types (single-select and radio-buttons look the same from a content point of view, just render differently on-screen).  My second guess is that it might be a string. 

The type determines how you compare it in "if" statements.  Select lists contain a single "option" (an object defined by Jira, not Java), and text contains java strings.

So, if you are coding for a

  • String, then I'd fall back to hard Java.
    • Change stuff like (cFieldValue?.value == "Yes") to ("Yes".equals(cFieldValue?.value)) - Java can be odd about using "==" to compare strings, whereas .equals always does what you'd expect, a direct character-to-character comparison.
  • Select list, again the string thing, but also you want to extract the display value of the option object, for which "getName()" is the most obvious function
    • Change stuff like (cFieldValue?.value == "Yes") to ("Yes".equals(cFieldValue?.value.getName()))

Note there are other field types with different content (multi-select/checkboxes contain arrays of options for example), but there's a very dull essay there.  Best to just look at what the fields you're working with are and get that working.

It is not a stupid question.  "New to the non-intuitive way things Jira does stuff" always gives us questions like this.  Don't expect that to ever go away - I try to answer questions here because when I'm stuck, the community helps me and I want to give back.  I'm still running into quirks I don't understand after 15ish years!

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 30, 2021

Have another look at this line:

def duedatetime = customFieldManager.getCustomFieldObject("customfield_11417")

This will fill the duedatetime variable with the custom field object - the "header" or "how this field works" definition of the field.  It is not the content of the field.

You'll need to use the variable to read the content for this issue, so try:

def duedatetimeCf = customFieldManager.getCustomFieldObject("customfield_11417")

def duedatetime = issue.getCustomFieldValue(customFieldCf)

Artem Semenov June 30, 2021

Hello @Nic Brough -Adaptavist- Thank you for responding!

I tried to do as you described, and it worked!

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import java.sql.Timestamp

def duedatetimeCf = customFieldManager.getCustomFieldObject("customfield_11417")
def duedatetime = issue.getCustomFieldValue(duedatetimeCf)
def noticket = new Timestamp((new Date() +1).getTime())

if ((duedatetime > noticket) || duedatetime == null)
return true

 

Previously, I used the following validator that worked for the duedate system field:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import java.sql.Timestamp

def duedate = issue.getDueDate()
def noticket = new Timestamp((new Date() +1).getTime())

if ((duedate > noticket) || duedate == null)
return true

 

Thank you so much for your help!

Like Nic Brough -Adaptavist- likes this
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 30, 2021

No problem.  This is one of those things that is not obvious until you've worked it out (or had someone tell you, like I did <mumble> years ago), but it'll stick with you for the rest of your scripting :-)

Like Artem Semenov likes this

Suggest an answer

Log in or Sign up to answer