Error while comparing date custom field with current date

Prashant Karakanagoudar March 23, 2021

Hi Team,

 

I am trying to add a scripted validator in workflow, to throw an error when customfield date is in future date (greater than current date).

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import groovy.time.TimeCategory
import java.util.Date
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
import java.text.SimpleDateFormat


def cf1 = getFieldByName("Change Start Date / Time")
Date date = new Date()

Date DateVal1 = (cf1.value as Date)
//Date DateVal2 = (cf2.value as Date)

if (DateVal1.getTime() > date) {
cf1.setError("Change Start Date Cannot be in future while moving to implemented")
}else{
cf1.clearError()
}

 

I am getting the below error:

2021-03-23 16:50:33,355 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed on issue MDTCM-2384 for user . View here: <jiraURL>
groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getFieldByName() is applicable for argument types: (String) values: [Change Start Date / Time]
at Script80.run(Script80.groovy:12)

 

Can someone help me to debug the issue here

 

 

Regards,

Prashant

 

 

2 answers

0 votes
Prashant Karakanagoudar May 20, 2021

I finally found easy way to add info to end user that Change the Start date in future by setting help text on the field.

 

Note: If there are two behaviours on same field and transition then you might see different behaviours/ errors. I encountered this issue on change start date, so i wrote same script for different field which was on the same screen to set helptext for field.


import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.fields.CustomField
import groovy.transform.Field

def logger = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
logger.setLevel(Level.INFO)

def issueManager = ComponentAccessor.getIssueManager();
def getChangeStartDateVal = getFieldById("customfield_10705").getValue();

logger.info("Obtained Change Start Date As "+getChangeStartDateVal);


def description = """Change start date is in future are you sure you want to move the change to implemented status"""

def currentDate = new Date()
//logger.info("Current Date",currentDate)

if (currentDate > getChangeStartDateVal) {
logger.info("Change Start Date is in Past")
}


else {
logger.info("Change Start Date is in Future")
getFieldById("customfield_10705").setHelpText(description)

}

0 votes
Prashant Karakanagoudar March 31, 2021

I am able to achieve the solution for the above query with a JQL validator.

 

However, we don't want the validator any more, but a pop-up message (warning) need to appear whenever the Custom Date field is greater than the current date during the transition.

Can we achieve this?

 

Regards,

Prashant

Prashant Karakanagoudar April 21, 2021

I am able to achieve this with behaviour. (However, behaviour is getting triggered for all transitions irrespective of conditions. Any help would be much appreciated.

Noticed the Behaviour won't work with the custom message field. So I have opted for a text field called Details and updating the required message to be displayed in it when the condition meets.

 

Script as below:

--------------------------------

def logger = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
logger.setLevel(Level.INFO)

def issueManager = ComponentAccessor.getIssueManager();
def getChangeStartDateVal = getFieldById("customfield_10705").getValue();

logger.info("Obtained Change Start Date As "+getChangeStartDateVal);


def alert = getFieldByName("Details")
def description = """Change start date is in future, are you sure you want to move the change to implemented status"""

def currentDate = new Date()
//logger.info("Current Date",currentDate)

if (currentDate < getChangeStartDateVal){
logger.info("Change Start Date is in Future")
alert.setHidden(true)

}
else {
logger.info("Change Start Date is in Past")
alert.setHidden(false)
.setFormValue(description)

}

-----------------------------------------

 

I have added this script against the 'change start date' field and with the condition when workflow action 'Change Implementation'.

Though script works fine, it's executing for other transitions where the 'change start date' field and 'Details' field is visible.

How to restrict it to only one particular transition?

 

 

Regards,

Prashant

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.13.2
TAGS
AUG Leaders

Atlassian Community Events