Set a field value based on created date/time/day plus X days depending of some condition

אריג' חמדאן January 9, 2019

Hello all,

I am looking to create a custom field that will be based on create date plus days. This field will be mostly used for counting time to close based on work hours days. if an issue is created between 8:00 to 18:00 the sla should show created date plus 6 days but if it',s created between 18:00 to 8:00 it should calculate created date plus 7 date.

Example would be:

Create date=1.1.19 8:00 + 6d = 7.1.19 18:00 [it should set always a specific hour 18:00 no matter when it was created.

Created date = 1.1.19 19:00 +7d = 8.1.19 18:00

 it should set the value of this addition in days and  a specific hour 18:00

 

Any suggestions would be greatly appreciated PLEASE.

Thanks

3 answers

0 votes
Vasiliy Zverev
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.
January 22, 2019

Have a look on comment to line 8 :)

0 votes
arige hamdan January 21, 2019

Hi,

After trying it, I noticed that in my create transition I have already built in the option of store.

See screenshot attached of your script and where I place it in my transition.

You can see also the "store" option below it.

 

The script still doesn't work.

 Your help and advise please

 

Thx!script 21.1.JPGcreate transition 21.1.JPG

0 votes
Vasiliy Zverev
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.
January 9, 2019

Dear arie, hi!

It seems that you need a custom postfuction on create trnasition to fill this date.

There are several plugins for this.

We use Script Runner.

Best regards, Vasiliy.

אריג' חמדאן January 10, 2019

Thank a lot

I use also scriptrunne but i m not familiar with it.

can you please help me and send me a script that i can run to get what i need as described upon?

 

thanks again

Vasiliy Zverev
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.
January 10, 2019

Here is a draft, 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.UpdateIssueRequest

import java.sql.Timestamp

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("issueKey"); //commnet into postfunction. For Script console only

Calendar newDate = Calendar.getInstance();
newDate.setTime(issue.getCreated());
int addDays = 6;
if ( (newDate.get(Calendar.HOUR_OF_DAY) < 8) & (newDate.get(Calendar.HOUR_OF_DAY) > 18) )
addDays = 7;

newDate.add(Calendar.DAY_OF_YEAR, addDays);

issue.setCustomFieldValue( ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("fieldName"), new Timestamp(newDate.getTimeInMillis()))

//Save chanes into DB. Only for script console
ComponentAccessor.getIssueManager().updateIssue(
ComponentAccessor.getJiraAuthenticationContext().getUser(),
issue,
UpdateIssueRequest.builder().sendMail(false).eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).build()
)
Like eleanor kerman likes this
אריג' חמדאן January 11, 2019

thanks a lot.

i will try it.

one more thing plz that i had forgot is that i need it to skip weekend days [friday and saturday] what should i change?

 

thanks again for your help

Vasiliy Zverev
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.
January 11, 2019

Into Calendar class you can get number on week. So, you need a while loop to get this date.

אריג' חמדאן January 11, 2019

hi

i had just run your script in my scripted field "due date" and i get this error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script208.groovy: 25: expecting ')', found '' @ line 25, column 1. 1 error

in addition i am not familiar with scrip runner so if i  should do something to make it run plz don't hesitate to write to me even if it is an obvious thing that everyone already know.

according to skip weekend can you please insert this change to your script please ?

here screenshot of my scripted field attached to this email:due date script.png 

thanks a lot for the help!

Vasiliy Zverev
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.
January 11, 2019

Here is version about hilidays:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.UpdateIssueRequest

import java.sql.Timestamp

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("issueKey"); //commnet into postfunction. For Script console only

Calendar newDate = Calendar.getInstance();
newDate.setTime(issue.getCreated());
int addDays = 6;
if ( (newDate.get(Calendar.HOUR_OF_DAY) < 8) & (newDate.get(Calendar.HOUR_OF_DAY) > 18) )
addDays = 7;

while (addDays > 0){
newDate.add(Calendar.DAY_OF_YEAR, 1);
if(newDate.get(Calendar.DAY_OF_WEEK) < Calendar.SATURDAY){
--addDays;
}
}

issue.setCustomFieldValue( ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("fieldName"), new Timestamp(newDate.getTimeInMillis()))

//Save chanes into DB. Only for script console
ComponentAccessor.getIssueManager().updateIssue(
ComponentAccessor.getJiraAuthenticationContext().getUser(),
issue,
UpdateIssueRequest.builder().sendMail(false).eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).build()
)
אריג' חמדאן January 12, 2019

THX!

After trying it i get this error:

 

java.lang.NullPointerException: Cannot invoke method getCreated() on null object
at Script426.run(Script426.groovy:11) :-(

Vasiliy Zverev
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.
January 13, 2019

So, 

when you use this script into postfunction you have a variable "issue" which resresents current issue.

When you run it frim script console you need to create this issue manually this is 11 line for. In this case you should enter proper issue key.

Like eleanor kerman likes this
אריג' חמדאן January 13, 2019

Thx for your answer.

I use this script into postfunction and i get that error, what am i doing wrong?

In addition i need also please that this script will give me a value with a specific time.

 See for Example:

Create date=1.1.19 8:00 + 6d = 7.1.19 18:30[it should set always a specific hour 18:30no matter when it was created.

Created date = 1.1.19 19:00 +7d = 8.1.19 18:30

 it should set the value of this addition in days and  a specific hour 18:00

I need the result to be set at 1830.

Can please add this to the script?

Thanks again

Best regard

Arige

Vasiliy Zverev
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.
January 14, 2019

Here you are

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.UpdateIssueRequest

import java.sql.Timestamp

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("issueKey"); //commnet into postfunction. For Script console only

Calendar newDate = Calendar.getInstance();
newDate.setTime(issue.getCreated());
newDate.set(Calendar.HOUR_OF_DAY, 18);
newDate.set(Calendar.MINUTE, 30);
newDate.set(Calendar.SECOND, 0);
int addDays = 6;
if ( (newDate.get(Calendar.HOUR_OF_DAY) < 8) & (newDate.get(Calendar.HOUR_OF_DAY) > 18) )
addDays = 7;

while (addDays > 0){
newDate.add(Calendar.DAY_OF_YEAR, 1);
if(newDate.get(Calendar.DAY_OF_WEEK) < Calendar.SATURDAY){
--addDays;
}
}

issue.setCustomFieldValue( ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("fieldName"), new Timestamp(newDate.getTimeInMillis()))

//Save chanes into DB. Only for script console
ComponentAccessor.getIssueManager().updateIssue(
ComponentAccessor.getJiraAuthenticationContext().getUser(),
issue,
UpdateIssueRequest.builder().sendMail(false).eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).build()
)
אריג' חמדאן January 14, 2019

Hi

Tnx.

I run this script in my post and unfortunately it doen't present a value in my custom field but when i run it from the consol it does.

I need this script to work from my post or from a scripted field so when an issue is created my custom field will take the value as configured in my post or scripted field.

In addition after running this script in my consol i realize that the given value don 't skip friday just saturday, so i need please to add to this script to skip also friday.

 

Please your help

And thanks a lot for your help and patient

Vasiliy Zverev
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.
January 15, 2019

On create transition you shoul add manually build-in postfunction "Store changes into database". It always added by defautl at all transitons. but not for create transition.

אריג' חמדאן January 15, 2019

But i need a script for create transition that can calculate created date as described in my first email.

Is it possible to get a script like that please?

any suggestion that can solve my problem please?

Build in postfunction manually doesn t give the due date time for all created issue.

Please any helthx

Vasiliy Zverev
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.
January 16, 2019

@אריג' חמדאן, you need to postfunctions:

first one to set date as I provided

second one to store changed values to DB. This is build-in postfunction is for.

אריג' חמדאן January 16, 2019

Thx

Can you please send me screenshots so i can see where and how it should look because i don t understand where i should set values to db...

Thx again

Vasiliy Zverev
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.
January 16, 2019

Dear @אריג' חמדאן, open you workflow, choice create transition and go to postfunction tab. You will see several postfunctions here. 

To realise you requarement you should manually add two postfunctions discribed above. See this: https://confluence.atlassian.com/adminjiraserver0710/advanced-workflow-configuration-953144832.html

arige hamdan January 16, 2019

create transition.JPGscript postfunction.JPGinline script.JPGhi

i know to do that

see screenshot below' i had set your script into inline script.

which another post i should choose from my postfunction? to set to it the second part of your script?

 

thx

best regard

Vasiliy Zverev
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.
January 16, 2019

This is for script postfunction and it is ok. Save it.

Open any ather postfunction and you will see in postfunction list store and reindex issue postfunctions. There no such on create transition. It means that you make changes to date filed, but not store it to DB.

So, you have to add it manually.

אריג' חמדאן January 17, 2019

I can't understand where and what i should add manually?

It should be done manually for every created issue? 

Thx

Vasiliy Zverev
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.
January 17, 2019

Dear Arige, let start once again.

Open postfunction tab on any transition and you will find a postfunction to store chages into database, exept create transiton. Lets suppose you have 5 postfunctions to change diffent custom values. If you want to have only one record into change history you should call store to DB only once.

After this open postfunction tab of create transition on any workflow. There is no store to DB postfunction. It means that any changes made by postfunctions will not be stores to DB.

So, on create transition press "Add postfunction" and choose one names like "Store changes to DB".

אריג' חמדאן January 19, 2019

Ok.

  • Can you plz send me a postfunction's screenshot where i can see this option: "store changes to db" because i had just done what you asked me to do step by step but unfortunately  couldn't find this post on my create transition.

Maybe a screenshot will help me to understand what i am missing.

Thx for your patient and your help.

אריג' חמדאן January 19, 2019

Another thing to clarify my case, i need this script to work just on my create transition else it won't answer my demand and i won't get my wishing calculated custum field that should be view on my created view screen.

Vasiliy Zverev
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.
January 20, 2019

Here you areimage.png

Suggest an answer

Log in or Sign up to answer