Forums

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

How to get Date picker custom filed value and compare it to system's date in groovy?

Shivaprasad Hattaraki
Contributor
May 7, 2018

I have custom field as named "WARRANTY DATE" (date picker) , i need to get this field value and have to compare with system's date.

If WARRANTY DATE is before than SYSTEM'S DATE it has to give not walid else valid.

plz help me 

I am new to groovy

4 answers

1 accepted

1 vote
Answer accepted
Marcos Sanchez
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.
May 7, 2018

Hi,

This code should work:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager

def warrantyField = customFieldManager.getCustomFieldObjectByName("WARRANTY DATE")

def warranty = issue.getCustomFieldValue(warrantyField)
def systemDate = new Date()

return warranty > systemDate

In this code, you take warranty date and system date and the return statement compares it. If warranty is before systemDate, it returns false and if warranty is after systemDate it returns true.



Regards,
Marcos

Shivaprasad Hattaraki
Contributor
May 7, 2018

i am getting error as static type checking  The variable (issue) is undeclared.

 

I have tried with underlyingissue and MutableIssue library but didnt work

Marcos Sanchez
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.
May 7, 2018

Hi,

 

Where are you running the script? Listener, Workflow, Console...?

 

Regards,

Marcos

Shivaprasad Hattaraki
Contributor
May 7, 2018

console

1 vote
Marcos Sanchez
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.
May 7, 2018

Hi,
Then you have to run this:

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager

def warrantyField = customFieldManager.getCustomFieldObjectByName("WARRANTY DATE")

def issue = issueManager.getIssueByCurrentKey("DEMO-123")

def warranty = issue.getCustomFieldValue(warrantyField)
def systemDate = new Date()

return warranty > systemDate


Replacing "DEMO-123" for the key of the issue you want to check.

You can use the first version of the code when using a Listener or a Workflow script.

Regards,
Marcos

Shivaprasad Hattaraki
Contributor
May 7, 2018

Thank you

 

I ll check

0 votes
Shivaprasad Hattaraki
Contributor
May 8, 2018

I am not getting output 

Although it is showing no error in executions.

 

Please help me to sort out this

Marcos Sanchez
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.
May 8, 2018

Hi,

In this image is how should it look to you after clicking "Run" on Script Console.

The output is in the "Result" tab.

 

2018-05-08_10h57_25.png

Regards,

Marcos

Shivaprasad Hattaraki
Contributor
May 8, 2018

yaa fine..

 

But do you knw how to execute this in script field using date picker custom field

I have tried it is showing invalid date

Shivaprasad Hattaraki
Contributor
May 8, 2018

I dont want to mention issue Key also. Ex(DEMO-123)

 

First version is fine but it has to show values valid or invalid when it compares.

Marcos Sanchez
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.
May 8, 2018

Hi,

Could you please explain me what do you want to do? 

As I understood, you have a field called "WARRANTY DATE" and you want to compare it with the system date.

What do you want to do with the result, or what else do you want to do?

 

Regards,
Marcos.

Shivaprasad Hattaraki
Contributor
May 8, 2018

I want to display result whether date is VALID or INVALID in view screen of all issues under that project

Marcos Sanchez
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.
May 8, 2018

Hi,

Then you can add a script field (with HTML template) named for example "WARRANTY" and use this code:

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager

def warrantyField = customFieldManager.getCustomFieldObjectByName("WARRANTY DATE")

def warranty = issue.getCustomFieldValue(warrantyField)
def systemDate = new Date()

if(warranty < systemDate){
return("<p style=color:red>INVALID</p>")
}else{
return("<p style=color:green>VALID</p>")
}

You should also configure it for the context and screen that you want it to work.

In this image you can see how did I configure the script field.

2018-05-08_14h06_04.png

Hope it helps.

Regards,
Marcos

Shivaprasad Hattaraki
Contributor
May 8, 2018

Thanks lot marcos 

Shivaprasad Hattaraki
Contributor
May 8, 2018

I am getting errors in 12 and 14th line as

cannot return value of type java.lang.string on method returning type java.lang.double

Capture1.PNG

Shivaprasad Hattaraki
Contributor
May 8, 2018

Its k Marcos 

It is working now.

Thanks lot

Greatly appreciate.

Marcos Sanchez
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.
May 9, 2018

Hi,

It's nice to see that it works.

Please approve the first answer so it's a valid one.

Regards,

Marcos

Shivaprasad Hattaraki
Contributor
May 11, 2018

Hi..

Marcos 

 Do u know how map this script using behaviours ?

Shivaprasad Hattaraki
Contributor
May 11, 2018

How to map fields 

Marcos Sanchez
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.
May 11, 2018

Hi,

What do you want to do with the behaviour?

The Script Field isn't enough for you?

 

Regards,
Marcos.

Shivaprasad Hattaraki
Contributor
May 11, 2018

Actually we don't want to add script field in that screen.

That's why i need to use behaviours.

Shivaprasad Hattaraki
Contributor
May 11, 2018

Do we able to show a result in that WARRANTY DATE field only? 

Shivaprasad Hattaraki
Contributor
May 11, 2018

By using this script and Behaviours can we able to pop up the Result in View screen ?

0 votes
Shivaprasad Hattaraki
Contributor
May 7, 2018

Getting error in return statement.

 

Cannot find matching method

Marcos Sanchez
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.
May 7, 2018

Please, paste the full error. 

Without the full error I can't know if it's a typo or a code error.

Thanks

Shivaprasad Hattaraki
Contributor
May 7, 2018

java.lang.Object#compareTo(java.util.Date). Please check if the declared type is right and if the method exists.

Marcos Sanchez
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.
May 7, 2018

Hi,

Did you run the script?

The error that you are reporting is the error that shows the red underlined >, right?

It's because, in script, warranty has no type defined, but in execution time it should work.

I'd just tried the script that I sent to you and it worked perfectly to me.

 

Regards,
Marcos

Shivaprasad Hattaraki
Contributor
May 8, 2018

Did you try this in customer linstener?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events