Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Scripting and Testing usign ScriptRunner - Calculate Differnce between 2 dates

aditya mandlekar February 9, 2018

Hi

I have an EPIC with 2 dates 1) Start Date 2) Live date. I need to find out difference between 2 dates. 

I understand scriptRunner Plugin Helps in creating custom Fields for this purpose

Steps Taken -

1. Custom Field create and added to screen

2. Inline script for calculation as below

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager

import java.util.Date.*

def customFieldManager = ComponentAccessor.getCustomFieldManager();


def dateFieldObject= customFieldManager.getCustomFieldObject('Live Date');
def dateFieldObject2= customFieldManager.getCustomFieldObject('Collections Start Date');

Issue issue = issue
issue = ComponentAccessor.issueManager.getIssueByCurrentKey("ISSUEKEY-1")

if(issue.getCustomFieldValue(dateFieldObject) ==null || issue.getCustomFieldValue
(dateFieldObject2)==null)
return null;
else
{
def dateValue= issue.getCustomFieldValue(dateFieldObject) as Date
def dateValue2= issue.getCustomFieldValue(dateFieldObject2) as Date
return dateValue - dateValue2
}

 

Current Error :

Cannot invoke method getCustomFieldValue() on null object

java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object
at Script160.run(Script160.groovy:14)

 

Query :

1. I understand above Error is because its not able to find out Live date and Start date are from EPIC from JIRA which is in Production Environment

2. if I have to Test this I need to Create above fields but I am unable to create EPIC in staging environment

Create Issue - UPG Toolchain Jira Staging #3

"Component/s" field is required and the project "Aditya Test Project" does not have any components.

I referred below useful link 

http://igorpopov.io/2014/11/24/rocking-with-jira-script-runner/

3.  How Can i Script where it can work for any 2 dates or as the name suggest its custom field calculating difference between 2 dates

4. Kindly suggest if the above code looks fine 

 

 

 

4 answers

Suggest an answer

Log in or Sign up to answer
0 votes
aditya mandlekar February 9, 2018

Thanks Clinton. Question How Can i test in Staging environment before It moves to Production.If I remove a line in staging environment I get an error stating 

 

Cannot invoke method getCustomFieldValue() on null object

java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object at Script280.run(Script280.groovy:14)

 

Code Snippet

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager

import java.util.Date.*

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateFieldObject= customFieldManager.getCustomFieldObjectByName('Live Date');
def dateFieldObject2= customFieldManager.getCustomFieldObjectByName('Start Date');
Issue issue;
//def issue = ComponentAccessor.issueManager.getIssueByCurrentKey("ATP-1")

if(issue.getCustomFieldValue(dateFieldObject) ==null || issue.getCustomFieldValue

(dateFieldObject2)==null)
return null;
else
{
def dateValue= issue.getCustomFieldValue(dateFieldObject) as Date
def dateValue2= issue.getCustomFieldValue(dateFieldObject2) as Date
return (dateValue - dateValue2)/30
}

CLINTON E. SMITH February 9, 2018

I have had some issues in the past where having my script resolve the getCFV() in the compare didn't work correctly as I would've expected.  I had to put them into variables first.  So my code would do the 'def dateValue...', and then do the 'if....' using those variables.  At least then I could put a forced 'return dateValue;', etc. after each 'def....' to make sure I was actually getting access to the data.  I have also experienced some headaches with the getCFObyName, getCFO('customfield_xxxxx') worked for me, so didn't bother to look and see why byName wasn't giving me what I wanted.

Wish I had a clearer answer.

aditya mandlekar February 11, 2018

Thanks Clinton. So my below code snippet which gives the error in Staging area will work fine ? Currently it throws Exception because of 2 reasons - it needs an EPIC on which getcustomerFieldsByName needs to be called upon 

 

ERROR-1.PNG

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager

import java.util.Date.*

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateFieldObject= customFieldManager.getCustomFieldObjectByName('Live Date');
def dateFieldObject2= customFieldManager.getCustomFieldObjectByName('Start Date');
/Issue issue;
//def issue = ComponentAccessor.issueManager.getIssueByCurrentKey("ATP-1")

if(issue.getCustomFieldValue(dateFieldObject) ==null || issue.getCustomFieldValue

(dateFieldObject2)==null)
return null;
else
{
def dateValue= issue.getCustomFieldValue(dateFieldObject) as Date
def dateValue2= issue.getCustomFieldValue(dateFieldObject2) as Date
return (dateValue - dateValue2)/30
}

0 votes
aditya mandlekar February 9, 2018

I have fixed all above issues. Thanks a ton Alexey for your quick help

I just need an help to make the above script more generic where ,it can work for any Current Key its working upon

 

issue = ComponentAccessor.issueManager.getIssueByCurrentKey("ISSUEKEY-1")

CLINTON E. SMITH February 9, 2018

issue should be predefined as the issue that is calling the script, so I am pretty sure if you remove:

Issue issue = issue
issue = ComponentAccessor.issueManager.getIssueByCurrentKey("ISSUEKEY-1")

Then it should just pick the issue you are working on.

0 votes
aditya mandlekar February 9, 2018

Thanks Alexey.  Few Query . When I am in Staging Environment and go to Custom Fields Page, I can see Live Date but not Collections Start date which is in Prod Enviornment.

Please suggest if other part of the code looks fine.  Also, my understanding is this script when it goes to production ,it will calculate "Elapsed Duration" - My custom scripted field Automatically as soon as Live date and Collections Start date is added

 

What changes I should have in my code where it refers to "Current EPIC Key".

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.
February 9, 2018

You should change

def dateFieldObject= customFieldManager.getCustomFieldObject('Live Date');
def dateFieldObject2= customFieldManager.getCustomFieldObject('Collections Start Date');

 

to

def dateFieldObject= customFieldManager.getCustomFieldObjectByName('Live Date');
def dateFieldObject2= customFieldManager.getCustomFieldObjectByName('Collections Start Date');
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.
February 9, 2018

As far as I understand there is only one question left. How to make the script more generic because right now you have an issue key defined in the script. It would depend on how you want to launch your script. Is it a scripted field , validator, listener or you want to launch it just from Script Console?

TAGS
AUG Leaders

Atlassian Community Events