behavior script get linked issues's latest duedate

echo lee August 7, 2018

// Get a pointer to the Linked Issues Text Area
//def linkedIssuesCF = getFieldById("issuelinks")
import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat;
def issueManager = ComponentAccessor.getIssueManager()
Date latest_begin_date = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar cal = Calendar.getInstance();

def linkedIssuesCF = getFieldById(getFieldChanged())
def issueLinksCF = getFieldById("issuelinks-linktype")
def linkedIssueForm = getFieldById("issuelinks-issues")
if ( issueLinksCF.getValue().toString() == "is blocked by" ) {
List <String> keys = linkedIssueForm.getValue()
for ( key in keys) {

def linkedIssue = issueManager.getIssueObject(key.toString())
if ( linkedIssue.dueDate.after(latest_begin_date) ){
latest_begin_date = linkedIssue.dueDate
}
def begin_date = getFieldByName("Begin Date")
cal.setTime(latest_begin_date);
cal.add(Calendar.DATE,1)
begin_date.setFormValue(cal.getTime())
}
}

 

but i can't not fetch issuekey via the statement:

linkedIssueForm.getValue()

3 answers

1 accepted

0 votes
Answer accepted
echo lee August 7, 2018

finally, the code block as follow is work.

List keys = []
import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat;
def issueManager = ComponentAccessor.getIssueManager()

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar cal = Calendar.getInstance();

def linkedIssuesCF = getFieldById(getFieldChanged())
def issueLinksCF = getFieldById("issuelinks-linktype")
def linkedIssueForm = getFieldById("issuelinks-issues")
if ( issueLinksCF.getValue().toString() == "is blocked by" ) {
if ( linkedIssueForm.getValue() instanceof java.util.List ) {
keys = ((String[])linkedIssueForm.getValue()).toList()
}else{
if (linkedIssueForm.getValue()) {
keys += (String)linkedIssueForm.getValue()
}
}
if ( ! keys.isEmpty() ) {
//linkedIssueForm.setError("keys is not empty!")
def latest_begin_date = sdf.parse(sdf.format(cal.getTime()))
for ( String key in keys) {
//linkedIssueForm.setError(key)
def linkedIssue = issueManager.getIssueObject(key)
if (! linkedIssue ) {
continue
}
if (! linkedIssue.dueDate ) {
continue
}
if ( linkedIssue.dueDate.after(latest_begin_date) ){
latest_begin_date = linkedIssue.dueDate
}
}
def begin_date = getFieldByName("Begin Date")
SimpleDateFormat sdf_begin = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf_date = new SimpleDateFormat("yyyy-MM-dd");
cal.setTime(sdf_begin.parse(sdf_date.format(latest_begin_date) +" 23:59:59"));
cal.add(Calendar.SECOND,1)
//begin_date.setError(sdf_begin.format(cal.getTime())+" "+cal.get(Calendar.DAY_OF_WEEK).toString())
if(cal.get(Calendar.DAY_OF_WEEK) == 7 ){
cal.add(Calendar.DATE,2)
}else if(cal.get(Calendar.DAY_OF_WEEK) == 0){
cal.add(Calendar.DATE,1)
}
begin_date.setFormValue(sdf_date.format(cal.getTime()))
}
}
1 vote
Mark Markov
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.
August 7, 2018

Hello @echo lee

This is happens because issuelinks is empty on form.

If you want get linked issues of current Issue, you ll need to use IssueLinkManager

It should be like this

// Get a pointer to the Linked Issues Text Area
//def linkedIssuesCF = getFieldById("issuelinks")
import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat;

Date latest_begin_date = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar cal = Calendar.getInstance();

def linkedIssuesCF = getFieldById(getFieldChanged())
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def linkedIssues = issueLinkManager.getOutwardLinks(underlyingIssue.id).findAll {it.issueLinkType.name.equalsIgnoreCase("is blocked by")}.collect {it.getDestinationObject()}
if (linkedIssues) {
for ( linkedIssue in linkedIssues) {
if ( linkedIssue.dueDate.after(latest_begin_date) ){
latest_begin_date = linkedIssue.dueDate
}
def begin_date = getFieldByName("Begin Date")
cal.setTime(latest_begin_date);
cal.add(Calendar.DATE,1)
begin_date.setFormValue(cal.getTime())
}
}
echo lee August 7, 2018

hello @Mark Markov

thanks for your suggestion. 

but i will using this script to provide my end user with a pre-filled date value based on the linked issue, when they add the issues to the form issuelinks‘s field issuelinks-issues, and the linktype is "is blocked by"

i found that, when i add one issue to the form, 

(String)linkedIssueForm.getValue() can return the issuekey, but when i add two issues to the form, linkedIssueForm.getValue() return a list, and i can't do with the list.

the modified code block as follow:

if (getAction()?.id != 1) {
return // not the initial action, so don't set default values
}
List keys = []
import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat;
def issueManager = ComponentAccessor.getIssueManager()

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar cal = Calendar.getInstance();

def linkedIssuesCF = getFieldById(getFieldChanged())
def issueLinksCF = getFieldById("issuelinks-linktype")
def linkedIssueForm = getFieldById("issuelinks-issues")
if ( issueLinksCF.getValue().toString() == "is blocked by" ) {
Date latest_begin_date = new Date();
if ( linkedIssueForm.getValue().getClass().isArray() ) {
keys = ((String[])linkedIssueForm.getValue()).toList()
}else{
keys += (String)linkedIssueForm.getValue()
}

for ( String key in keys) {
linkedIssueForm.setError(key)
def linkedIssue = issueManager.getIssueObject(key)
if (! linkedIssue ) {
continue
}
if (! linkedIssue.dueDate ) {
continue
}
if ( linkedIssue.dueDate.after(latest_begin_date) ){
latest_begin_date = linkedIssue.dueDate
}
}

def begin_date = getFieldByName("Begin Date")
cal.setTime(latest_begin_date);
cal.add(Calendar.DATE,1)
begin_date.setFormValue(cal.getTime())
}

 

 

the code block as follow can't work. 

if ( linkedIssueForm.getValue().getClass().isArray() ) {
keys = ((String[])linkedIssueForm.getValue()).toList()
}else{
keys += (String)linkedIssueForm.getValue()
}

0 votes
Tom Moors
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!
August 7, 2018

Hi @echo lee,

Do you want to provide your end user with a pre-filled date value based on the linked issue that they can change when editing the issue?

If so, you might want to make use of the underlyingissue (see https://community.atlassian.com/t5/Adaptavist-questions/ScriptRunner-Behaviours-how-to-get-information-of-an-issue-when/qaq-p/625146 for an example), to get the issue object. Starting with that one, you can just fetch all the information using the Jira api instead of parsing values from the form.

Other question:

- Do you use this block as a behaviour validator or an initialiser?

- Is it an option to provide a script listener that will automatically fill in those values upon updating the issue?

 

Kind regards,

 

Tom

echo lee August 7, 2018

Thanks Tom. 

yes, i want to provide my end user with a pre-filled begin_date based on the linked issue , when they add the linked issues. 

if there are linktype 'is blocked by" issues exists, begin_date = blockissues's latest_duedate + 1

otherwise, begin_date is today.

- Do you use this block as a behaviour validator or an initialiser?

i use this block as a behaviour service side scripts.

- Is it an option to provide a script listener that will automatically fill in those values upon updating the issue?

no.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events