I would like to write a script which calclautes the differnce between two date fields and display the result as number of days in a custom text field.

Field 1 : start date

Field 2 : end date

Field 3 . total number of days.

Expected result:

Filed 3 = field 2 - field 1

i used the following script

<script type="text/javascript">
var startdate = new Date();
var enddate = new Date();
var days = 0;
startdate = document.getElementById('Start Date').value;
enddate = document.getElementById('End Date').value;
days = enddate.getTime()-startdate.getTime();
document.getElementById('Total Number of Days').values = days;
</script>

i used the follwing link as the reference :

https://confluence.atlassian.com/jirakb/how-to-display-a-field-based-on-another-field-s-selection-649921383.html

2 answers

1 accepted

0 votes
Accepted answer
Tarun Sapra Community Champion Nov 27, 2017

You can in the "post-function" calculate the different in dates of two fields and then populate the required custom field in the post-function transition, thus it will save you from writing JS as things happen in the postfunction.

Hi Tarun,

Does the change happen immediately as i change the values of the field 1 and field 2 or it happens after submitting the request.

Tarun Sapra Community Champion Nov 27, 2017

It will happen after submitting the request, but the field can be hidden on create screen and populated in the post-function so that it's visible on the view screen

Hi Tarun, 

Thabks for the reply. could you please elaborate on this, i am bit confused

i tried by adding the script to the post function of  the transition by selecting "update issue custom field", but the entire script is visisble on the view screen instead of a particular value.

 

please correct me if i have done anything worng.

Tarun Sapra Community Champion Nov 27, 2017

Can you share screenshot as to where you are adding the script.

You have to add the script in the post-function of "Script Post-Function" and then "Custom script post-function"

Tarun Sapra Community Champion Nov 27, 2017

It's pretty much wrong at many levels, you are inserting JS in the post-function which is not required, try to put  the script as i have mentioned in the previous comment in the "Script post-function" and then custom script post-funciton

i am new to jira service desk . can you tell me where can i find script post function and custom script post function.

@Tarun Sapra where should i add these scripts?

Tarun Sapra Community Champion Nov 27, 2017

First, please check if you have "script runner" plugin installed, if not then the approach I suggested to you isn't possible as it requires an plugin.

i dont have script runner, i chked for the pricing and stuf.. is there any other workaround..

Tarun Sapra Community Champion Nov 27, 2017

Without the plugin, the only way to do it is to use simple Javascript which you have already shared in the question. But this is not supported by Atlassian.

The JS code which you have written, doesn't take into account the "change"/ "blur" events i.e. when do you want the field's value to be filled.

So you have to write an JS which gets triggered when dates in both fields are filled and then you calculate the days and fill that value in 3rd field. But for that you have to register event listener like in the link which you have shared there is an *onchange()* event listener in the method.

@Tarun Sapra Thank you for the help...

hi @Tarun Sapra I tried the script runner plugin but i was not able to get the result i wanted 

// custom field references

// date picker 1 
def startdate = 'customfield_10401'  

// date picker 2

def enddate = 'customfield_10403'

// text field where i want my result of the date difference calculation

def totaldays = 'customfield_10406'

// Extract the existing values from the issue
def sdate = issue.fields[startdate] as Integer
def edate = issue.fields[enddate] as Integer
if (sdate == null && edate == null) {
// No date's was specified, we can't calculate the date
return
}
def tdays = issue.fields[totaldays] as Integer

// Calculate the days
def days = edate-sdate

put("/rest/api/2/issue/${issue.key}")
//.queryString("overrideScreenSecurity", Boolean.TRUE)
.header("Content-Type", "application/json")
.body([
fields:[
(totaldays): days
]
])
.asString()

 

 

thats the script i used..

0 votes
Alexey Matveev Community Champion Nov 27, 2017

Hello, It must work in portal? 

Hi Alexey,

Yeah i need this to work on create issue screen (customer portal)

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 13, 2018 in Jira Service Desk

Jira Service Desk – Don’t be afraid, the journey begins with curiosity!

...be more productive while being fun to use at the same time. For some, getting started can be a bit intimidating. This is especially true if Jira Service Desk is your first exposure to Atlassian...

36,627 views 13 39
Read article

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