Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

script to set the date based on custom field value

Srikanth Gogineni April 19, 2019

Hi ,

Please find the below scenario

we have two custom fields.

Field A the value = 2 

Field B ( Date field)

Now i need to set the date ( exclude Saturday and Sunday ) for the custom field B as current date + 2 ( which means if today is Monday then field B value should be Thursday's date)

Can any one help with the post script for the above scenario.

Thanks

2 answers

1 accepted

0 votes
Answer accepted
Antoine Berry
Community Champion
April 19, 2019

Hi @Srikanth Gogineni ,

This code should do the trick (credits to this post) : 

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.component.ComponentAccessor

int numberFieldId = 13100
int dateFieldId = 13101

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def numberField = customFieldManager.getCustomFieldObject(numberFieldId)
def dateField = customFieldManager.getCustomFieldObject(dateFieldId)

def numberFieldValue = issue.getCustomFieldValue(numberField)
def dateFieldValue = issue.getCustomFieldValue(dateField)

Calendar c1 = GregorianCalendar.getInstance();
c1.setTime(dateFieldValue)

for(int i=1;i<=numberFieldValue;i++){
c1.add(Calendar.DAY_OF_MONTH, 1);
if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
c1.add(Calendar.DAY_OF_MONTH, 1);
if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
c1.add(Calendar.DAY_OF_MONTH, 1);
}

dateField.updateValue(null, issue, new ModifiedValue(dateFieldValue, c1.getTime().toTimestamp()), new DefaultIssueChangeHolder())

Of course update custom field ids.

Antoine

Srikanth Gogineni April 22, 2019

Hi @Antoine Berry 

Thank you very much for your code and it is working fine but there is small issue with the script.

Date field is not getting calculated properly when i move to previous/other transitions and changing the "Numberfieldvalue" . it is increment from the existing set date to new date ( not from the current date ).

example : 

Current Date : 4/22/2019

Numberfieldvalue : 2 

Set Date : 4/24/2019 ( after executing the script)

Now if i go back and change the numberfieldvalue to 1 then the new Set Date is getting updated to 4/25/2019( 2 +1 ) instead of 4/23/2019.

 

please help on the same.. Thanks 

Antoine Berry
Community Champion
April 23, 2019

Hi @Srikanth Gogineni ,

Sorry did not catch it was current date + 2. Just replace the setTime line with this : 

c1.setTime(new Date())

and it should work fine.

Antoine

Srikanth Gogineni April 23, 2019

Hi @Antoine Berry ,,

 

Thanks a lot . Your script worked like a charm... Thank you very much.

 

Regards

Srikanth

Like Antoine Berry likes this
0 votes
Alexey Matveev
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.
April 19, 2019

Hello,

If you already have a scripting plugin, then you should add a tag with the plugin to your question, because there are multiple plugins available and you should provide info for the one you have.

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, jira service management, jsm webinar, ai in jsm, opsgenie integration, incident management, virtual agent, atlassian intelligence, ai-powered service desk, it operations, atlassian learning, service management webinar, team '25 recap

What’s new in Jira Service Management 🤔

Discover how Atlassian is revolutionizing service management with cutting-edge solutions for AI-powered support, HR Service Management, or DevOps connectivity.

Register here ⬇️
AUG Leaders

Atlassian Community Events