Forums

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

Display respective label if date is closer to due date in ScriptRunner

Jeremy Chee
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 21, 2023

I'm still learning how to create custom script fields on ScriptRunner (JIRA) and I'm working on this exercise where I am required to query for an issue's due date and display the respective label when it approaches the due date:
- "Green" - 10 days from the due date
- "YELLOW" - over 4 days but less than 10 days away from the due date
- "RED" - less than 4 days from the due date

1 answer

1 accepted

0 votes
Answer accepted
Jeremy Chee
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 24, 2023

Here's the solution to my question if anyone is curious 


```

import java.time.Duration;
import java.time.LocalDateTime;
import groovy.xml.MarkupBuilder

def dueDateField = issue.getDueDate();
def fieldContent = "";

if(!dueDateField) {
    log.warn("Due date doesn't exist on this item")
    fieldContent = "Due date doesn't exist on this item"
    return;
}

def currDate = LocalDateTime.now();
def daysRemaining = Duration.between(currDate, dueDateField.toLocalDateTime()).toDays();
def messageColour = "";
StringWriter writer = new StringWriter()
def builder = new MarkupBuilder(writer)

log.warn("due date field: $dueDateField, current date: $currDate");
log.warn("difference: $daysRemaining");

if(!dueDateField) {
    // log.warn("Due date doesn't exist on this item")
    fieldContent = "Due date doesn't exist on this item"
    return;
}

if(daysRemaining <= 4) {
    messageColour = "error"
    fieldContent = "You have less than 4 days left till this ticket is due!"
} else if (daysRemaining >= 10) {
    messageColour = "success"
    fieldContent = "There is still $daysRemaining days remaining until this ticket is due."
} else {
    messageColour = "warning"
    fieldContent = "There is only $daysRemaining days remaining until this ticket is due."
}

builder.div(class: "aui-message aui-message-$messageColour shadowed") {
    p(class: 'title') {
        strong("$fieldContent")
    }
}

writer.toString()

```

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events