Forums

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

Set due date from a Post function using Scriptrunner

Patrik Silverby June 21, 2022

Hi!

I am trying to update due date for an issue on a specific transition using post function and Scriptrunner. I have the below code wish is accepted and executed ok. However nothing happens with the Due date field. I am new to this and having a hard time navigating the documentation around it. I have read different posts on the subject and by that collected the code below. Any ideas on what I am doing wrong?

Date date = new Date();
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.YEAR, 1);
c.add(Calendar.MONTH, 0);
c.add(Calendar.DATE, 0);
c.add(Calendar.HOUR, 0);
c.add(Calendar.MINUTE, 0);
c.add(Calendar.SECOND, 0);
Date dateInTheFuture = c.getTime();
issue.fields.duedate = dateInTheFuture;

Transition post functions.jpeg CodeWindow.jpeg

1 answer

0 votes
padraik
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.
June 21, 2022

Here's what I use for this - I'm on Server, so you may have to use the documentation to make a minor tweak if something doesn't work on cloud

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import static java.lang.Math.*
import java.sql.Timestamp

 

// get current issue

def cfManager = ComponentAccessor.customFieldManager

def dateField = cfManager.getCustomFieldObject(12839) //use the ID of your "Due Date" field

 

// get today's date

def today = new java.sql.Timestamp(new Date().getTime())

 

// set value

issue.setCustomFieldValue(dateField, today)

return true

Patrik Silverby June 27, 2022

Thank you for your reply! Is this the approach even if the field is a custom field? I am trying to update the standard due date field on an issue.

padraik
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.
June 27, 2022

oh, sorry, that's a system field - you can just use:

issue.setDueDate(today)

Patrik Silverby June 27, 2022

I have tried that but the function setDueDate is not recognized. 


SetDueDate.png

 

I also just tried your last two lines

def today = new java.sql.Timestamp(new Date().getTime())

issue.setDueDate(today)

but it yields the same result.

padraik
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.
June 27, 2022

make sure you're casting your issue to a MutableIssue type -

try something like this:


import com.atlassian.jira.issue.MutableIssue

...

date dateInTheFuture = c.getTime()

def myIssue = issue as MutableIssue

myIssue.setDueDate(date)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
atlassian, loom, loom for training, loom for teaching, video training, async learning, online education, screen recording, loom tutorials, loom use cases, atlassian learning, team training tools, instructional video, virtual training tools

🛗 Elevate Your Training and Enablement with Loom

Join us June 26, 11am PT for a webinar with Atlassian Champion Robert Hean & Loom’s Brittany Soinski. Hear tips, stories, and get your burning questions answered. Learn how Loom makes training and enablement easier. Don’t miss it!

Register today
AUG Leaders

Atlassian Community Events