Forums

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

I am using a script to set the due date based on a custom field value

G Sunil Kumar March 26, 2021

Hello, 

I am using a scripted post function to setup the due date but I keep getting else value being set even if the value is Four Months

 

Script:-

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import java.text.SimpleDateFormat
import com.atlassian.jira.issue.comments.CommentManager
import java.sql.Timestamp
import com.atlassian.core.util.DateUtils
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def userUtil = ComponentAccessor.getUserUtil()

//def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
//log.setLevel(Level.INFO)

MutableIssue mutableIssue = issue

def RiskValidity = customFieldManager.getCustomFieldObject("customfield_19703")
def RiskValidityV = issue.getCustomFieldValue(RiskValidity)
log.info("The value is " + RiskValidityV)

if(RiskValidityV == "Two Months"){
issue.setDueDate(new Timestamp((new Date() + 60).time))
}
else if (RiskValidityV == "Four Months"){
issue.setDueDate(new Timestamp((new Date() + 120).time))
}
else if (RiskValidityV == "Six Months"){
issue.setDueDate(new Timestamp((new Date() + 180).time))
}
else {
issue.setDueDate(new Timestamp((new Date() + 180).time))
}

4 answers

2 accepted

3 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
March 28, 2021

Hi Sunil,

From your code, it appears that you are adding just the number of days to the current date. Instead, it would help if you tried to convert it to milliseconds first.

You could try out this working code:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

import java.sql.Timestamp

def mutableIssue = issue as MutableIssue
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager

def validity = customFieldManager.getCustomFieldObjectsByName("Validity")[0]
def validityValue = validity.getValue(mutableIssue).toString()

def timeInMillis = (24 * 60 * 60 * 1000) as Long

if(validityValue == "Two Months") {
mutableIssue.setDueDate(new Timestamp( System.currentTimeMillis() + (timeInMillis * 60) as Long ))
} else if(validityValue == "Four Months") {
mutableIssue.setDueDate(new Timestamp( System.currentTimeMillis() + (timeInMillis * 120) as Long ))
} else if(validityValue == "Six Months") {
mutableIssue.setDueDate(new Timestamp( System.currentTimeMillis() + (timeInMillis * 180) as Long ))
} else {
mutableIssue.setDueDate(new Timestamp( System.currentTimeMillis() + (timeInMillis * 180) as Long ))
}

issueManager.updateIssue(loggedInUser, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)

Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.

Also, please try using the Issue Manager to update the Issue, i.e.:-

issueManager.updateIssue(loggedInUser, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)

to save your changes.

 

Below are some print screens of the test results for your reference:-

image1.png

 

image2.png

 

image3.png

I hope this helps to solve your question :)


Thank you and Kind regards,

Ram

1 vote
Answer accepted
Will C
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.
March 27, 2021

Hi there, on my phone so don't have access to a code editor but I believe this is because you are comparing RiskValidityV which is a different class to the string. Try RiskValidityV.toString() == "Two Months" and let us know if that works? If it doesn't can you run RiskValidityV.class and let us know what class it is?

0 votes
G Sunil Kumar March 28, 2021

Hello Everyone,

 

Thank you for your inputs.

 

I was able to achieve it.

 

Regards,

Sunil

0 votes
John Funk
Community Champion
March 26, 2021

Hi Sunil,

Why don't you just use Automation for Jira to set the Due Date for you? 

Suggest an answer

Log in or Sign up to answer