Forums

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

Need help with a Script Runner groovy script. I'm writing a simple scripted validator.

Jason Stein
Contributor
April 25, 2018

We have an "IT Change Management" project.  The simple scripted validator is supposed to check to see if a custom field (date field) called "Outage End" will be within date/time of 2 other custom date fields, the "Implementation Start" and the "Implementation End".  I also have another simple scripted validator called "Outage Start" which does very similar thing.  So what I have works for this.  Here's the script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField ImplStartField = customFieldManager.getCustomFieldObject("customfield_13933");
CustomField ImplEndField = customFieldManager.getCustomFieldObject("customfield_13932");

CustomField OutageStartField = customFieldManager.getCustomFieldObject("customfield_17037");
CustomField OutageEndField = customFieldManager.getCustomFieldObject("customfield_17038");

Date ImplStart = (Date)issue.getCustomFieldValue(ImplStartField)
Date ImplEnd = (Date)issue.getCustomFieldValue(ImplEndField)
Date OutageStart = (Date)issue.getCustomFieldValue(OutageStartField)
Date OutageEnd = (Date)issue.getCustomFieldValue(OutageEndField)

if (OutageEnd.after(ImplStart))
{
OutageEnd.before(ImplEnd) || OutageEnd.compareTo(ImplEnd) == 0
}
else
{
return 0
}

 

My problem is that on certain changes, there is no outage - so the requirement is now that I need to test to see if the "Outage End" field is null and if it is, bypass the if statement entirely, as a null value is acceptable.  I would obviously do this with the "Outage Start" as well.

2 answers

1 accepted

0 votes
Answer accepted
Jason Stein
Contributor
April 27, 2018

I figured out the issue.  It was on what I was returning.  In this case, "0" does not equal "true".

Here's the code that I ended up with which has been tested:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField ImplStartField = customFieldManager.getCustomFieldObject("customfield_13933");
CustomField ImplEndField = customFieldManager.getCustomFieldObject("customfield_13932");

CustomField OutageStartField = customFieldManager.getCustomFieldObject("customfield_17037");
CustomField OutageEndField = customFieldManager.getCustomFieldObject("customfield_17038");

Date ImplStart = (Date)issue.getCustomFieldValue(ImplStartField)
Date ImplEnd = (Date)issue.getCustomFieldValue(ImplEndField)
Date OutageStart = (Date)issue.getCustomFieldValue(OutageStartField)
Date OutageEnd = (Date)issue.getCustomFieldValue(OutageEndField)

if (!(OutageEnd) && !(OutageStart)) {
return true
} else if (OutageEnd.after(ImplStart)) {
OutageEnd.before(ImplEnd) || OutageEnd.compareTo(ImplEnd) == 0
} else {
return false
}
0 votes
Nic Brough -Adaptavist-
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 25, 2018

There's a really easy trick here:

if ( variable ) {do something}

The if will execute when the variable is "true", meaning it has content.  If it's empty (null) or contains a boolean false, then the if will not.

Jason Stein
Contributor
April 25, 2018

Nic, thank you for your quick response.  I believe that I understand what you said.  However, I'm having a hard time executing it.  My java/groovy knowledge isn't too great, I'm afraid.  Here's what I'm doing:

I've added the following before the if statement above:

CustomField omCf = customFieldManager.getCustomFieldObjectByName('Outage Start')
def om = issue.getCustomFieldValue(omCf)

if (om == null)
{ return 0
}
else
{

       if (OutageEnd.after(ImplStart))
       {
       OutageEnd.before(ImplEnd) || OutageEnd.compareTo(ImplEnd) == 0
       }
       else
       {
       return 0
       }

}

 

My guess is that "om " doesn't really equal null or I'm not evaluating it properly, but I don't know.

Nic Brough -Adaptavist-
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 25, 2018

I think your problem is outside the "if" parts.  I know coders have different styles and preferences, but I'll write how I would do it (bear in mind I am a long way off an expert coder!  I used to be, but in a totally different language, which I've not touched for 16 years)

if ( !om )
{

       if (OutageEnd.after(ImplStart))
       {

       if (  OutageEnd.before(ImplEnd) || OutageEnd.compareTo(ImplEnd) == 0 )

          { return 1 }
       }

}

return 0  //default return, when om has any value, or (om has a value and the other compares fail)

Jason Stein
Contributor
April 25, 2018

Nic, thanks again.

I put in your code and I'm still not getting a satisfactory result.  :-(

Here's what it says in the log for that specific validator:

2018-04-25 14:47:51,022 ERROR [utils.ConditionUtils]: *************************************************************************************
2018-04-25 14:47:51,023 ERROR [utils.ConditionUtils]: Condition failed on issue: ITCM-10945, built-in script:com.onresolve.scriptrunner.canned.jira.workflow.validators.SimpleScriptedValidator
java.lang.NullPointerException: Cannot invoke method before() on null object
 at Script402.run(Script402.groovy:24)
2018-04-25 14:47:51,024 ERROR [utils.ConditionUtils]: Script follows:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField ImplStartField = customFieldManager.getCustomFieldObject("customfield_13933");
CustomField ImplEndField = customFieldManager.getCustomFieldObject("customfield_13932");
CustomField OutageStartField = customFieldManager.getCustomFieldObject("customfield_17037");
CustomField OutageEndField = customFieldManager.getCustomFieldObject("customfield_17038");
Date ImplStart = (Date)issue.getCustomFieldValue(ImplStartField)
Date ImplEnd = (Date)issue.getCustomFieldValue(ImplEndField)
Date OutageStart = (Date)issue.getCustomFieldValue(OutageStartField)
Date OutageEnd = (Date)issue.getCustomFieldValue(OutageEndField)
CustomField omCf = customFieldManager.getCustomFieldObjectByName('Outage Start')
def om = issue.getCustomFieldValue(omCf)
if (!om)
{    if (OutageStart.before(ImplEnd))
 {
 if ( OutageStart.after(ImplStart) || OutageStart.compareTo(ImplStart) == 0 )
  { return 1 }
    }
}
return 0
Jason Stein
Contributor
April 25, 2018

I realized that this was for the "Outage Start" one, not the "Outage End" one.  But logically, they should be the same.

Nic Brough -Adaptavist-
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 25, 2018

That implies OutageEnd is empty - that's a separate field to outage start.  My guess is you need to check if either of them are empty, not just start.

Jason Stein
Contributor
April 25, 2018

Nic, yes, you're right, but I'm still getting a NPE.  It's failing even though it's only looking at one field.  

Nic Brough -Adaptavist-
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 25, 2018

The current error is that "outagestart" has no content.  You could avoid that error with another check:

if ( ! (om && OutageStart) )

Although that assumes that if outagestart is empty as well, it succeeds.

Jason Stein
Contributor
April 25, 2018

But my requirement is that the user will leave it as no content, and I should let that be, and not require anything more. 

As it stands now, the Simple Scripted Validator "Error Message" keeps showing if they leave it blank. 

I want them to be able to leave it blank (null) or if they put anything in the field, it's got to be within the Implementation Start/End date/time.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events