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 :
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Tarun,
Thabks for the reply. could you please elaborate on this, i am bit confused
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
i am new to jira service desk . can you tell me where can i find script post function and custom script post function.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
i dont have script runner, i chked for the pricing and stuf.. is there any other workaround..
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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..
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello, It must work in portal?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Alexey,
Yeah i need this to work on create issue screen (customer portal)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.