Using Script Runner for scheduled issue creation

Osama Al-Areky February 4, 2024

Hello,

I am trying to program issue creation according to the input of the user on a date field on an existing issue. The user select either one date, and the issue is created on that that date, or selects a frequency and how many times (daily, weekly, monthly) so that the a new issue is created repeatedly according to that frequency (ex, weekly, 4 times), or select a period, and the issue is created every day of this period. I have selected the following code template but still doesn't work and nothing happens when executed:

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.issueManager
def userManager = ComponentAccessor.userManager
def issueFactory = ComponentAccessor.issueFactory
def customFieldManager = ComponentAccessor.customFieldManager
def constantsManager = ComponentAccessor.constantsManager

def currentUser = userManager.getUserByName("admin") // Replace with the appropriate username
def project = issue.getProjectObject()

// Get values from custom fields
def singleDate = customFieldManager.getCustomFieldObjectByName("Single Date").getValue(issue)
def frequency = customFieldManager.getCustomFieldObjectByName("Frequency").getValue(issue)
def numOccurrences = customFieldManager.getCustomFieldObjectByName("Number of Occurrences").getValue(issue)
def startDate = customFieldManager.getCustomFieldObjectByName("Start Date").getValue(issue)
def endDate = customFieldManager.getCustomFieldObjectByName("End Date").getValue(issue)

// Logic to create issues based on the selected options
def dateToCreateIssue = null

if (singleDate) {
    dateToCreateIssue = singleDate
} else if (frequency && numOccurrences) {
    // Create issues based on frequency and number of occurrences
    for (int i = 0; i < numOccurrences; i++) {
        if (dateToCreateIssue) {
            dateToCreateIssue = incrementDate(dateToCreateIssue, frequency)
        } else {
            dateToCreateIssue = startDate
        }

        createNewIssue(project, dateToCreateIssue)
    }
} else if (startDate && endDate) {
    // Create issues for each day within the date range
    dateToCreateIssue = startDate
    while (dateToCreateIssue <= endDate) {
        createNewIssue(project, dateToCreateIssue)
        dateToCreateIssue = incrementDate(dateToCreateIssue, "daily")
    }
}

// Function to increment the date based on frequency
Date incrementDate(Date date, String frequency) {
    Calendar calendar = Calendar.getInstance()
    calendar.setTime(date)

    if (frequency == "daily") {
        calendar.add(Calendar.DAY_OF_MONTH, 1)
    } else if (frequency == "weekly") {
        calendar.add(Calendar.WEEK_OF_YEAR, 1)
    } else if (frequency == "monthly") {
        calendar.add(Calendar.MONTH, 1)
    }

    return calendar.getTime()
}

// Function to create a new issue
void createNewIssue(project, dateToCreateIssue) {
    def newIssue = issueFactory.getIssue()
    newIssue.setIssueTypeId("yourIssueType") // Replace with your issue type
    newIssue.setProjectObject(project)
    newIssue.setSummary("New issue created on ${dateToCreateIssue}")
    newIssue.setDescription("Description goes here")
    
    // Set other fields as needed

    issueManager.createIssueObject(
        currentUser,
        newIssue
    )
}

2 answers

0 votes
Uday Kiran Bhaviri
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 4, 2024

Hi @Osama Al-Areky  when this code will run or what is the triggering point? 

Did you put any logs to troubleshoot?

1. Add logs to your code.

2. Also try to add try catch blocks

3. First try with one condition and check whether it's working or not

4. Make sure where and when you triggering this code to run.. this is important.

0 votes
Craig Nodwell
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 4, 2024

Hi @Osama Al-Areky what is the platform you are on?  What do you mean nothing happens?  Where are you putting this code?  Are you getting any logs output?

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events