It's not the same without you
Join the community to find out what other Atlassian users are discussing, debating and creating.
Hi Guys,
I am looking to script a validator to allow transition only if the time value in a date/time custom field is within a specified range, or alternatively to prevent the transition if the entered time is between 4-5pm. I am unsure how to script this for a custom date/time picker field.
Currently I have written a script using the following language:
(
(cfValues['Environment']*.value.contains("UAT")) &&
((cfValues['Proposed Time of Recycle'].getTimeString() >= '04:00:00 PM') && (cfValues['Proposed Time of Recycle'].getTimeString() << '05:00:00 PM'))
)
But in testing transition is being allowed at anytime.
Please can someone let me know what kind of syntax and script operators I should be using to build this kind of script.
Thanks,
David
You can do this by adding a custom scripted validator and getting the hour from the date like in the following example:
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.Issue import com.opensymphony.workflow.InvalidInputException def issue = issue as Issue def customFieldManager = ComponentAccessor.getCustomFieldManager() // replace name with your field name def dateTimeCf = customFieldManager.getCustomFieldObjectByName("Proposed Time of Recycle") def dateTime = new Date(issue.getCustomFieldValue(dateTimeCf).getTime() as Long) def hour = dateTime.toCalendar().get(Calendar.HOUR_OF_DAY) // Check hour is not between 4pm to 5pm if (hour == 16) { throw new InvalidInputException("DateTime should not be between 4-5pm!") }
I'm not sure what type the "Environment" field is but you need to get the value using the customFieldManager in the same way as the other field and then add the condition to the if statement. The cfValues variable is not available in the binding.
Thanks,
Adam
If its single select its just:
def environmentCf = customFieldManager.getCustomFieldObjectByName("Environment") def environment = issue.getCustomFieldValue(environmentCf) if (hour == 16 && enviroment == "UAT") { throw new InvalidInputException("DateTime should not be between 4-5pm!") }
If its multi-select it should be:
def environmentCf = customFieldManager.getCustomFieldObjectByName("Environment") def environments = issue.getCustomFieldValue(environmentCf) if (hour == 16 && "UAT" in environments*.value) { throw new InvalidInputException("DateTime should not be between 4-5pm!") }
To get the minute you need:
def minute = dateTime.toCalendar().get(Calendar.MINUTE)
Hi @Adam Markham [Adaptavist] ,
I have the same requirement here :
Based on the "hour" value I want to update a select list field "Shift" which has options shift 1, shift 2 etc.
When I use the following code, I get error when trying to extract the "Time" value from date value.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException
def issue = issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
// replace name with your field name
def dateTimeCf = customFieldManager.getCustomFieldObjectByName("Proposed Time of Recycle")
//def dateTime = new Date(issue.getCustomFieldValue(dateTimeCf).getTimeString())
def dateTime = new Date(issue.getCustomFieldValue(dateTimeCf).getTime() as Long)
def hour = dateTime.toCalendar().get(Calendar.HOUR_OF_DAY)
// Check hour is not between 4pm to 5pm
if (hour == 16) {
throw new InvalidInputException("DateTime should not be between 4-5pm!")
}
The line
def dateTime = new Date(issue.getCustomFieldValue(dateTimeCf).getTime() as Long)
Is showing error :
Could you please help me on this?
Thanks
Swathi
This is just a static type checking error, when it runs it will work.
To make the type checker happy, we have to tell it what type the value of the date time field is. Date time fields in JIRA give you a java.sql.Timestamp.
Your script should be like the following:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException
import java.sql.Timestamp
def issue = issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
// replace name with your field name
def dateTimeCf = customFieldManager.getCustomFieldObjectByName("Proposed Time of Recycle")
def dateTime = new Date((issue.getCustomFieldValue(dateTimeCf) as Timestamp).getTime() as Long)
def hour = dateTime.toCalendar().get(Calendar.HOUR_OF_DAY)
// Check hour is not between 4pm to 5pm
if (hour == 16) {
throw new InvalidInputException("DateTime should not be between 4-5pm!")
}
This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.
Read moreAtlas Camp is our developer event which will take place in Barcelona, Spain from the 6th -7th of September . This is a great opportunity to meet other developers and get n...
Connect with like-minded Atlassian users at free events near you!
Find a groupConnect with like-minded Atlassian users at free events near you!
Unfortunately there are no AUG chapters near you at the moment.
Start an AUGYou're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.