Hi I am trying to set the due date on an issue based on the date entered in the Received Date custom field when the issue is created. I have used the code below but it consistently seems to be setting a date int he passed rather than 30 working days from the received date. Any help would be appreciated.
import org.apache.log4j.Category
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.CustomFieldType
import com.atlassian.jira.issue.fields.CustomField
import java.sql.Timestamp;
import org.joda.time.DateTime;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.type.EventDispatchOption
//Issue issue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def dateRecDate = customFieldManager.getCustomFieldObjectByName("Received Date")
def dateRecValue = issue.getCustomFieldValue(dateRecDate) as Date
int days = 30;
int dayOfWeek = 0;
Date today = new Date();
MutableIssue myIssue = issue
Calendar dueDate = Calendar.getInstance();
while ((dayOfWeek == 0) || (dayOfWeek == Calendar.SATURDAY) || (dayOfWeek == Calendar.SUNDAY))
{dueDate.setTimeInMillis(dateRecValue.getTime() + days*1000*24*60*60);
dayOfWeek = dueDate.get(Calendar.DAY_OF_WEEK);
days = days + 1;
}
System.out.println("Script setduedate is running...and mydueDate is:" + dueDate.getTime());
myIssue.setDueDate (new Timestamp (dueDate.getTime().getTime()));
ComponentAccessor.getIssueManager().updateIssue(
ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
, issue
, EventDispatchOption.ISSUE_UPDATED
, false
)
Here is refactored code
import com.atlassian.jira.issue.UpdateIssueRequest
import java.sql.Timestamp;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.type.EventDispatchOption
//Issue issue = issue
int days = 30;
Calendar dueDate = Calendar.getInstance();
dueDate.setTime( (Date) issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Received Date")));
while ((days == 0) || (dueDate.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) || (dueDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY))
{
dueDate.add(Calendar.DAY_OF_YEAR, 1);
--days;
}
System.out.println("Script setduedate is running...and mydueDate is:" + dueDate.getTime());
issue.setDueDate (new Timestamp (dueDate.getTime().getTime()));
ComponentAccessor.getIssueManager().updateIssue(
ComponentAccessor.getJiraAuthenticationContext().getUser()
, issue
, UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build() //compatability with JIRA 7 API
)
Thank you for the quick response but I am afraid this code doesn't work. Having tested i this morning all it does is copy the Received Date to the Due Date and produce this error on the post function;
2018-03-07 07:27:24,568 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-03-07 07:27:24,568 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.IllegalArgumentException: Source GenericValue can not be null.
at com.atlassian.jira.association.NodeAssociationStoreImpl.getSinksFromSource(NodeAssociationStoreImpl.java:34)
at com.atlassian.jira.project.version.DefaultVersionManager.getVersionsByIssue(DefaultVersionManager.java:750)
at com.atlassian.jira.project.version.DefaultVersionManager.getFixVersionsFor(DefaultVersionManager.java:595)
at com.atlassian.jira.project.version.DefaultVersionManager.updateIssueFixVersions(DefaultVersionManager.java:565)
at com.atlassian.jira.issue.fields.FixVersionsSystemField.updateIssueValue(FixVersionsSystemField.java:98)
at com.atlassian.jira.issue.fields.AbstractVersionsSystemField.updateValue(AbstractVersionsSystemField.java:368)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:704)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:669)
at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:219)
at com.atlassian.jira.issue.IssueManager$updateIssue$3.call(Unknown Source)
at Script481.run(Script481.groovy:26)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Into postfunction you should comment
ComponentAccessor.getIssueManager().updateIssue(
ComponentAccessor.getJiraAuthenticationContext().getUser()
, issue
, UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build() //compatability with JIRA 7 API
)
This is only for running into console. There is a postfunction "store changes into a databasee" is used into postfunction list for a transition
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You see, when you want just to test some scrpt it is simpler to open script console and run script from there. In this case you need:
When you run a postfunction you aloredy have variable "issue" which represent current issue and into list of postfuntions you always have postfuntion to store changes into database.
So this code is for postfunction
import com.atlassian.jira.issue.UpdateIssueRequest
import java.sql.Timestamp;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.type.EventDispatchOption
//Issue issue = issue //tjis is for console
int days = 30;
Calendar dueDate = Calendar.getInstance();
dueDate.setTime( (Date) issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Received Date")));
while ((days == 0) || (dueDate.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) || (dueDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY))
{
dueDate.add(Calendar.DAY_OF_YEAR, 1);
--days;
}
//System.out.println("Script setduedate is running...and mydueDate is:" + dueDate.getTime());
issue.setDueDate (new Timestamp (dueDate.getTime().getTime()));
//this is only for console
/*ComponentAccessor.getIssueManager().updateIssue(
ComponentAccessor.getJiraAuthenticationContext().getUser()
, issue
, UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build() //compatability with JIRA 7 API
)*/
And this one for Script Console
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.UpdateIssueRequest
import java.sql.Timestamp;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.type.EventDispatchOption
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("issue key") //tjis is for console
int days = 30;
Calendar dueDate = Calendar.getInstance();
dueDate.setTime( (Date) issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Received Date")));
while ((days == 0) || (dueDate.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) || (dueDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY))
{
dueDate.add(Calendar.DAY_OF_YEAR, 1);
--days;
}
//System.out.println("Script setduedate is running...and mydueDate is:" + dueDate.getTime());
issue.setDueDate (new Timestamp (dueDate.getTime().getTime()));
//this is only for console
ComponentAccessor.getIssueManager().updateIssue(
ComponentAccessor.getJiraAuthenticationContext().getUser()
, issue
, UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build() //compatability with JIRA 7 API
)
Replace "issue key" with actual issue key and this script will run for this issue.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you for the clarification I really appreciate it.
Whilst the postfunction code above no longer gives an error it is only copying the Received date to the Due date field, it is not adding on the 30 working days.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Dear Adam,
try to debug this script into Script Console.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can you explain to me the section of the code that is adding on the 30 days? I have run the code over and over again in Console but it just returns the same date every time equal to that in Received date.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It seems to be a trouble with condition in while.
Here is updated code:
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.UpdateIssueRequest
import java.sql.Timestamp;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.type.EventDispatchOption
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("issue key") //tjis is for console
int days = 30;
Calendar dueDate = Calendar.getInstance();
dueDate.setTime( (Date) issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Received Date")));
while (days != 0)
{
dueDate.add(Calendar.DAY_OF_YEAR, 1);
if((dueDate.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) || (dueDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY))
continue;
--days;
}
//System.out.println("Script setduedate is running...and mydueDate is:" + dueDate.getTime());
issue.setDueDate (new Timestamp (dueDate.getTime().getTime()));
//this is only for console
ComponentAccessor.getIssueManager().updateIssue(
ComponentAccessor.getJiraAuthenticationContext().getUser()
, issue
, UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build() //compatability with JIRA 7 API
)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.