The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

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

Scriptrunner script to validate difference between start and end time is not more than 3 months

Minu Murali
Contributor
September 13, 2021

Hi Team,

I am working on scriptrunner on JSM data center.

I am looking for a Scriptrunner script to validate difference between start and end time is not more than 3 months

Please advise.

 

Thanks

 

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

3 votes
Answer accepted
Leo
Community Champion
September 13, 2021

Hi @Minu Murali,

You can get difference b/w 2 days by normal subtraction

endDate - startDate

 or you can use below method too

//assume you fetched start and end dates already
startDate.setMonth(startDate.getMonth() + 3)
if(endDate > startDate){
//end date is 3 months greater than start Date
}else{
// end date is less than 3 months
}

 hope this helps

 

BR,

Leo

0 votes
Minu Murali
Contributor
September 16, 2021

Hi ,

I have to add one more condition to the above snippet which is to ignore a particular request type.

When I combine this condition with the above condition, it is not working.Both of them are working successfully if added separately.

import com.atlassian.jira.component.ComponentAccessor

def cfm = ComponentAccessor.getCustomFieldManager()
def start = issue.getCustomFieldValue(cfm.getCustomFieldObjectsByName("Start Date")[0])
def end = issue.getCustomFieldValue(cfm.getCustomFieldObjectsByName("End Date")[0]) 

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Customer Request Type")
def requestType = issue.getCustomFieldValue(cf)



start.setMonth(start.getMonth() + 3)
if ((requestType.toString != “ims/b3270e4e-6b55-4e59-b58e-8d2720a10905”) && (end > start)){
     return false
}else{
     return true
}

 

Could you please tell me why this is not working?

The expected result is if the request type is equal to the given one, it should go to else and return true.

But it still goes to check the start and end condition. ( we do not have start and end dates in this request type form)

Thanks!

0 votes
Minu Murali
Contributor
September 13, 2021

Hi Leo,

 

Thanks for the prompt reply. I am fairly new to ScriptRunner so I am having difficulties.

I tried the above script but do not understand what to change where?

I managed to get this working almost 90%.

cfValues["End Date"] - cfValues ["Start Date"] <=3

is what I am using. However the 3 is defaulting to 3 days. How do I make it 3 months. ( and both my custom fields are Date and Time fields).

Leo
Community Champion
September 13, 2021

you may need to add couple of lines to include given mechanism. can you try below snippet

import com.atlassian.jira.component.ComponentAccessor

def cfm = ComponentAccessor.getCustomFieldManager()
def start = issue.getCustomFieldValue(cfm.getCustomFieldObjectsByName("Start Date")[0])
def end = issue.getCustomFieldValue(cfm.getCustomFieldObjectsByName("End Date")[0])

start.setMonth(start.getMonth() + 3)
if(end > start){
return false
}else{
return true
}

 

BR,

Leo

Minu Murali
Contributor
September 13, 2021

Thanks a ton @Leo . This worked perfectly well.

TAGS
AUG Leaders

Atlassian Community Events