It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Using Scripts

Karthik Mohan Nov 27, 2017

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
Answer accepted
Tarun Sapra Community Leader 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.

Karthik Mohan Nov 27, 2017

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 Leader 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

Karthik Mohan Nov 27, 2017

Hi Tarun, 

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

Karthik Mohan Nov 27, 2017 • edited

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 Leader 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"

Karthik Mohan Nov 27, 2017

image.png

Tarun Sapra Community Leader 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

Karthik Mohan Nov 27, 2017

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

Karthik Mohan Nov 27, 2017

@Tarun Sapra where should i add these scripts?

Tarun Sapra Community Leader 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.

Karthik Mohan Nov 27, 2017

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

Tarun Sapra Community Leader 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.

Karthik Mohan Nov 27, 2017

@Tarun Sapra Thank you for the help...

Karthik Mohan Nov 28, 2017

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 [cPrime] Community Leader Nov 27, 2017

Hello, It must work in portal? 

Karthik Mohan Nov 27, 2017

Hi Alexey,

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

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Jira Service Desk

4 changes to queues and issues you don’t want to miss

Hello Atlassian Community! I'm Tania, a Senior Product Marketing Manager for Jira Service Desk Cloud! I'm excited to announce some exciting improvements coming up for those who work with queues and...

660 views 9 13
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you