add days to date by behaviour or script runner

A November 3, 2015

Requriement

1) Add 10 days to custom date field.

2) 10 days will be pulled from a custom field ( number)

Tried

Below step using behavior plugin but not working

 

import com.atlassian.jira.component.ComponentAccessor

import static com.atlassian.jira.issue.IssueFieldConstants.*

def desc = getFieldById("customfield_11002")

def defaultValue = getFieldById("customfield_11003")

def newvalue = defaultValue.getFormValue() as Long

desc.setFormValue(newvalue)

return

 

I am not able to access the value of customfield_11003 to store in customfield_11002

 

 Not interested in using misc custom fields as it is messing up with my custom field view

 

 

 

6 answers

4 votes
Thanos Batagiannis _Adaptavist_
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.
November 4, 2015

Hi Armar,

For the searcher use the Date Time Range picker. For the template use Date Time Picker.

And in the inline field add the logic. In your case should be something like: 

def dateField = getCustomFieldValue("DateCF")
def numberField = getCustomFieldValue("NumberCF")
def number = numberField?.intValue()

if (number == null || dateField == null)
    return

def result = dateField + number
return new Date(result.getTime())

Please let me know if this works for you.

Regards,

Thanos

Thanos Batagiannis _Adaptavist_
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.
November 4, 2015

Amar, As I said, the solution I propose is to use scripted field (https://scriptrunner.adaptavist.com/latest/jira/scripted-fields.html) and not a behaviour. As far as I understand you want a custom field which will hold the value of two other custom fields (in your case a date plus a number).

A November 4, 2015

ok... do i need to import any headers

Thanos Batagiannis _Adaptavist_
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.
November 4, 2015

You don't have to import anything in the above script.

A November 4, 2015

/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml [onresolve.scriptrunner.customfield.GroovyCustomField] getCustomFieldValue called from script: No field with ID or name customfield_10620 /sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml [onresolve.scriptrunner.customfield.GroovyCustomField] getCustomFieldValue called from script: No field with ID or name customfield_11106

Thanos Batagiannis _Adaptavist_
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.
November 4, 2015

Apparently there are no custom fields assigned to you project with such names. Please try to add as a param in getCustomFieldValue the name and no the id of the custom field.

A November 4, 2015

you mean def dateField = getCustomFieldValue("Planned date") def numberField = getCustomFieldValue("Number of days") def number = numberField?.intValue() if (!number || !dateField) return def result = dateField + number return new Date(result.getTime()) instead of def dateField = getCustomFieldValue("customfield_11111") def numberField = getCustomFieldValue("customfield_11234") def number = numberField?.intValue() if (!number || !dateField) return def result = dateField + number return new Date(result.getTime())

A November 4, 2015

IT works!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :-)

A November 4, 2015

Now how can i skip the weekend days.. I mean considering only working days.

Thanos Batagiannis _Adaptavist_
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.
November 4, 2015

I suppose the logic should be something like that (in pseudocode): while (i < number) { dateField ++ if (dateField == sunday || dateField == saturday) i -- i ++ }

A November 4, 2015

Thank will work on it... thanks alot for quick reply and complete solution

A November 5, 2015

Hi Thanus. /*import com.atlassian.jira.issue.MutableIssue import java.sql.Timestamp; MutableIssue myIssue = issue; def dateField = getCustomfieldValue("Date") Calendar dateField = Calendar.getInstance() def numberfield = getcustomFieldValue("Days") def number = numberfield?.intValue() if number (!number || !dateField) return dateField.add(Calendar.DATE, number) while ([Calendar.SATURDAY, Calendar.SUNDAY].contains(dateField.get(Calendar.DAY_OF_WEEK))) { def dateField.add(Calendar.DATE,number) } def result = new Timestamp(dateField.gettime()) return new Date(result.gettime())*/ Not working

0 votes
amar November 3, 2015

Thanos, Is it possible to add the date and number and get a output.. If yes please let me know which import option I need to choose Regards Amar

0 votes
Thanos Batagiannis _Adaptavist_
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.
November 3, 2015

Amar, You can have a look at https://scriptrunner.adaptavist.com/latest/jira/scripted-fields.html I think a scripted field that depends on the result of 2 other custom fields is what you want. Please let me know if you need any help with the script part Cheers Thanos

0 votes
A November 3, 2015

Hi Thanos, I want to use 1 custom date field value and add it with 2nd custom number field content and populate result into 3rd custom field

0 votes
Thanos Batagiannis _Adaptavist_
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.
November 3, 2015

Hi Amar, Could you please be more specific in what you are trying to achieve, maybe using a scripted field (if you are using script runner) ? What type are your custom fields ?

0 votes
Junaid Shah
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.
November 3, 2015

Have you tried using the JIRA Workflow Toolbox plugin and using the Post Function Math & Date-Time Expression Calculator

I think that will do exactly what you are looking for.

 

In the Post Function you can use something like

 

datePart({Customfield_ID(Date)}, LOCAL) + ({CustomField_ID(Number)} * {DAY})

A November 3, 2015

Hi Sy, Thanks for the quick reply. I want the instance update, not transition based update

Suggest an answer

Log in or Sign up to answer