How to Populate Day in a Custom Field based on Date Picker Custom Field Value in JIRA

Still need help?

The Atlassian Community is here for you.

Ask the community

Purpose

The purpose of this article is to show how to automatically populate a custom field based on the chosen date from a Date Picker custom field. This can be done by using a third-party plugin; ScriptRunner for JIRA.

(info) This configuration was tested on JIRA 6.4.4 using ScriptRunner 3.0.16

Solution

The information in this page relates to customisations in JIRA. Consequently, Atlassian Support cannot guarantee to provide any support for the steps described on this page as customisations are not covered under Atlassian Support Offerings. Please be aware that this material is provided for your information only and that you use it at your own risk.

Also, please be aware that customisations done by directly modifying files are not included in the upgrade process. These modifications will need to be reapplied manually on the upgraded instance.

  • Make sure you have ScriptRunner for JIRA plugin installed in your JIRA. If you don't have the plugin installed, please refer to Installing Marketplace apps to install ScriptRunner for JIRA.
  • Add a Custom Field with type Date Picker. Place it on the "Create Issue Screen". This field will take input from users to get the date value. In this example, the name of the custom field is "Test Date".

  • Add a Custom Field with type Scripted Field. The custom field type Scripted Field is provided by ScriptRunner for JIRA so you'll need to install ScriptRunner before adding this field. Do not place this field on any screens as the purpose of this field is being auto-populated based on the value of the "Test Date" field.

  • Run the following SQL query against JIRA's database to return the custom field ID:

     select id,cfname from customfield;

    In this example, the results we get are:

      id   |        cfname
    -------+-----------------------
     10100 | Customer Request Type
     10101 | Request participants
     10102 | Time to resolution
     10005 | Test Date
     10006 | Test Day
    (5 rows)

    (info) In our example above the Custom field ID for "Test Date" is 10005.

  • Navigate to Add-Ons > Script Fields. Notice that the "Test Day" custom field will be available in Scriptrunner Script Fields. Click on "Edit" to add the JavaScript from below.

  • Choose the option "Text Field (multi-line)" as the "Template" and paste the following Javascript in as a "Inline script".

    import com.atlassian.jira.ComponentManager
    import com.atlassian.jira.issue.CustomFieldManager
    import com.atlassian.jira.issue.fields.CustomField
    
    
    ComponentManager componentManager = ComponentManager.getInstance()
    CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
    CustomField cf= customFieldManager.getCustomFieldObject("customfield_10005") 
    def value = issue.getCustomFieldValue(cf)
    
    
    Calendar c = Calendar.getInstance()
    c.setTime(value)
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK)
    
    
    if (dayOfWeek == 1)
    	return "Sunday"
    if (dayOfWeek == 2)
    	return "Monday"
    if (dayOfWeek == 3)
    	return "Tuesday"
    if (dayOfWeek == 4)
    	return "Wednesday"
    if (dayOfWeek == 5)
    	return "Thursday"
    if (dayOfWeek == 6)
    	return "Friday"
    if (dayOfWeek == 7)
    	return "Saturday"
Last modified on Nov 2, 2018

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.