Forums

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

How to use if statement in workflow post function

Alan Sim
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!
February 1, 2023

I have an incident level: P1, P2 or P3 which user must input in the workflow after open status. I would like to set:

if P1, due date = now + 2 hours

if P2, due date = now + 1 day

if P3, due date = now + 2 days.

 

Is there a way to do this in workflow post functions? Thanks

3 answers

2 accepted

5 votes
Answer accepted
Karim ABO HASHISH
Community Champion
February 1, 2023

Hi @Alan Sim 

You can do it by creating project automation rule (check below). i just want to remind you that due date is a date field not Date time, so you won't have the precision of time in hours. The workaround here is to create a new custom field of type DateTime to use it instead.

ProTip: you can switch between function now.PlusDays and nowPlusBusinessDays according to your support level 24/7 or normal business days.

firefox_HisrYpobf0.gif

Let me know if you have any questions

Alan Sim
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!
February 1, 2023

Thanks a ton Karim. The project automation works.

0 votes
Answer accepted
Simen Eriksen February 1, 2023

Hi Alan! I am not sure if that is possible using post functions, at least not what I can recall or find after a quick check.

What you can do, is use project automation for that. 
You can have the trigger to be when a new issue is created, or when it transitions (I am not sure from your question what fits better). 

Then you can have a check if P1, P2 or P3 is selected, and accordingly you can set the due date.

Would this be an option for you? 

0 votes
Sachin Dhamale
Community Champion
February 1, 2023

Hi Alan,

Use this code to add the hours to the current date

def hoursToIncrement = 2
def date = new Date()
def calendar = Calendar.getInstance()
calendar.time = date
calendar.add(Calendar.HOUR_OF_DAY, hoursToIncrement)
def incrementedDate = calendar.time

Use this code to add the days to the current date

def daysToIncrement = 1
def date = new Date()
def calendar = Calendar.getInstance()
calendar.time = date
calendar.add(Calendar.DAY_OF_YEAR, daysToIncrement)
def incrementedDate = calendar.time

 

and use the rest API in the post function to update the due date with the above's values.

// Specify the issue key to update
def issueKey = '<IssueKeyHere>'

// Get today's date to set as the due date
def today = new Date()

// Update the issue
def result = put("/rest/api/2/issue/${issueKey}")
.header('Content-Type', 'application/json')
.body([
fields:[
// Set the due date to today's date
duedate: today.format('yyyy-MM-dd') as String
]
])
.asString()

// Validate the issue updated correctly
if (result.status == 204) {
return "Success - The issue with the key of ${issueKey} has been updated with a new due date"
} else {
return "${result.status}: ${result.body}"
}

 

Thanks,

Sachin

Simen Eriksen February 1, 2023

Hi Sachin, just a question, you are setting today's date as the expiry date. And you are not informing what language you are using (Groovy).

Your code does not seem to do what he asks for.

Sachin Dhamale
Community Champion
February 1, 2023

@Simen Eriksen 

Its Groovy he can use in the post function ( As he asked for) on Open status. 

In the Code We are taking current date and increment it by hours/days and then we can set that date to due date custom field.

Its just Not a complete code he need to use as per his requirement.

Provided he will need a Script Runner Plugin.

 

 

Thanks,

Sachin

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