add days to date by behaviour or script runner

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

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

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).

ok... do i need to import any headers

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

/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

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.

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())

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

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

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

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

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

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})

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

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 ?

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

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

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted 9 hours ago in Jira Service Desk

Looking for anyone who has switched from Zendesk to Jira Service Desk

Hi Community! The Jira Service Desk marketing team is looking for customers who have successfully switched from Zendesk to Jira Service Desk!   We’d love to hear your thoughts on the pros and ...

15 views 0 1
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you