Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Date Time Picker Validator

Henry Lin July 29, 2020 edited

hi guys, 

 

I need some help on a date time picker validator; We have 2 date time pickers (Maintenance - Start Time, Maintenance - End Time), my only requirement is to validate if selected Maintenance - Start Time and Maintenance - End Time have been booked? If it's booked, then prevent it from creating (a new issue) and display a message ("this time slot is booked"), otherwise, ok to create a new issue.

 

regards, 

Henry

1 answer

0 votes
Martin Bayer _MoroSystems_ s_r_o__
Community Champion
July 29, 2020

@Henry Lin what does it mean "booked"? What would this condition look like? Let's say we have

  • issue to be created -> issue_to_be_created
  • existing issues

So the condition should be

  • is there any issue in set of existing issues which has 
    • start date >= issue_to_be_created.start date
    • AND
    • end data <=  issue_to_be_created.end date
  • if condition is true, do not let the user to create new issu

Do I understand it correctly?

Henry Lin July 29, 2020

hi @Martin Bayer _MoroSystems_ s_r_o__ , pretty closed! This validation checks the date and the time in one project; for example, let's say we have an event scheduled at 8pm - 10pm July 30th (under project X), and someone wants to schedule another event at 9pm of July 30th on create issue screen (of project X), the screen should show an error message ("this time has been booked or it's not available, please selected another time"), and prevent the issue to be created. 

we have Scriptrunner in place, but not sure how to validate the date and time. Much appreciate your help! 

 

regards, 

Henry

Martin Bayer _MoroSystems_ s_r_o__
Community Champion
July 30, 2020 edited

hi @Henry Lin I would prepare JQL which will return issues which matches condition you described. So there must not be any existing issue which has Start or End date in range of Start-End of issue to be created (you need to use scripted validator)

  • import com.atlassian.jira.component.ComponentAccessor
    import com.atlassian.jira.issue.search.SearchProvider
    import com.atlassian.jira.jql.parser.JqlQueryParser
    import com.atlassian.jira.web.bean.PagerFilter
    def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
    def searchProvider = ComponentAccessor.getComponent(SearchProvider)
    def issueManager = ComponentAccessor.getIssueManager()
    def customFieldManager = ComponentAccessor.getCustomFieldManager()
    def user = ComponentAccessor.getJiraAuthenticationContext().getUser() // edit this query to suit

    def dateTimeFormat = "yyyy-MM-dd HH:mm"
    def maintenanceStartCF = customFieldManager.getCustomFieldObject(xxxxL)
    def maintenanceStartDate = issue.getCustomFieldValue(maintenanceStartCF)
    def maintenanceStartString = maintenanceStartDate.format(dateTimeFormat)

    def maintenanceEndCF = customFieldManager.getCustomFieldObject(xxxxL)
    def maintenanceEndDate = issue.getCustomFieldValue(maintenanceEndCF)
    def maintenanceEndString = maintenanceEndDate.format(dateTimeFormat)

    def jqlQuery = 'project = X AND ("Maintenance - Start Time">=$maintenanceStartString AND "Maintenance - Start Time"<=$maintenanceEndString OR "Maintenance - End Time">=$maintenanceStartString AND "Maintenance - End Time"<=$maintenanceEndString )'
    def query = jqlQueryParser.parseQuery(jqlQuery)

    def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter()) log.debug("Total issues: ${results.total}")
    if(results.total > 0) {
    throw new InvalidInputException(maintenanceStartCF.id, "this time has been booked or it's not available, please selected another time")
    }

     

I don't have any server environment to test the script so can you test it?

Henry Lin August 9, 2020

hi @Martin Bayer _MoroSystems_ s_r_o__ , appreciate the code! I'll test out the code when i get back from the vacation in a week, and I will let you know how it goes! 

 

regards, 

Henry

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, jira product discovery, jpd premium, product management, idea management, product discovery, jira premium, product planning, atlassian community, product development, roadmap planning, product prioritization, feature management

Introducing Jira Product Discovery Premium ✨

Jira Product Discovery Premium is now available! Get more visibility, control, and support to build products at scale.

Learn more
AUG Leaders

Atlassian Community Events