Forums

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

Set due date in behaviour -Script Runner

Marcela Junyent
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 5, 2020

I have the following Script Runner - Behaviours:

def dueDateField = getFieldByName("Fecha de entrega")
def dueDateField2 = getFieldByName("Due Date")
def riskLevel = getFieldByName("Risk Rating").getValue()

def dateToSet

if (riskLevel == "1. Critical") {
dateToSet = new Date() + 7
} else if (riskLevel == "2. High") {
dateToSet = new Date() + 30
} else if (riskLevel != "3. Medium") {
dateToSet = new Date() + 90
}else if (riskLevel != "3. Medium") {
dateToSet = new Date() + 180
}

dueDateField.setFormValue(dateToSet.format("dd/MMM/yy"))
dueDateField2.setFormValue(dateToSet.format("dd/MMM/yy"))

 

It works for custom field Fecha de Entrega, But not for Due Date field.

 

I can't find my error!!

I'm new with script runner.   

2 answers

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 29, 2020

Hi Marcela,

From your code, the main reason the Due Date field is not updating is because you are invoking it using getFieldByName i.e. 

def dueDateField2 = getFieldByName("Due Date")


Because this is a system field, you should instead invoke the field by using the getFieldById i.e.

def dueDateField2 = getFieldById("duedate")


Another point, for system fields the name should be in lower case.  If you notice above I am using getFieldById("duedate")  the duedate is fully in lowercase.

Also, if you are trying to update the date, you should use milliseconds instead of days.

For the Behaviour configuration, since your value is depending on the change of the Risk Level field, it would be better to use a Server Side Behaviour configuration.

Below is a print screen of the same Server Side Behaviour configuration:-

behaviour_config.png

Below is a sample code for your reference:-

import java.sql.Timestamp

def riskLevel = getFieldById(fieldChanged)

def
riskLevelValue = riskLevel.value.toString()

def dueDateField = getFieldByName("Fecha de entrega")

def dueDateField2 = getFieldById("duedate")

def days = 24*60*60*1000 // number of milliseconds in a day

def updatedDate = new Date()

def timePeriod // to pass the number of days.

if (riskLevelValue == "1. Critical") {
timePeriod = 7
} else if (riskLevelValue == "2. High") {
timePeriod = 30
} else if (riskLevelValue == "3. Medium") {
timePeriod = 90
} else {
timePeriod = 180
}


updatedDate.setTime(new Timestamp(System.currentTimeMillis() + timePeriod * days).time)


dueDateField.setFormValue(updatedDate.format("dd/MMMM/yyyy"))

dueDateField2.setFormValue(updatedDate.format("dd/MMMM/yyyy"))


Hope this helps solve your question :)

 

Cheers,

Ram

1 vote
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 29, 2020

Hi Marcela,

From your code, the main reason the Due Date field is not updating is because you are invoking it using getFieldByName i.e. 

def dueDateField2 = getFieldByName("Due Date")


Because this is a system field, you should instead invoke the field by using the getFieldById i.e.

def dueDateField2 = getFieldById("duedate")


Another point, for system fields the name should be in lower case.  If you notice above I am using getFieldById("duedate")  the duedate is fully in lowercase.

Also, if you are trying to update the date, you should use milliseconds instead of days.

For the Behaviour configuration, since your value is depending on the change of the Risk Level field, it would be better to use a Server Side Behaviour configuration.

Below is a print screen of the same Server Side Behaviour configuration:-

behaviour_config.png

Below is a sample code for your reference:-

import java.sql.Timestamp

def riskLevel = getFieldById(fieldChanged)

def
riskLevelValue = riskLevel.value.toString()

def dueDateField = getFieldByName("Fecha de entrega")

def dueDateField2 = getFieldById("duedate")

def days = 24*60*60*1000 // number of milliseconds in a day

def updatedDate = new Date()

def timePeriod // to pass the number of days.

if (riskLevelValue == "1. Critical") {
timePeriod = 7
} else if (riskLevelValue == "2. High") {
timePeriod = 30
} else if (riskLevelValue == "3. Medium") {
timePeriod = 90
} else {
timePeriod = 180
}


updatedDate.setTime(new Timestamp(System.currentTimeMillis() + timePeriod * days).time)


dueDateField.setFormValue(updatedDate.format("dd/MMMM/yyyy"))

dueDateField2.setFormValue(updatedDate.format("dd/MMMM/yyyy"))


Hope this helps solve your question :)

 

Cheers,

Ram

Suggest an answer

Log in or Sign up to answer