How to throw exception in Jira UI if some condition is satisfied in groovy script

shashank vivek November 25, 2015

Although I can see error in catilina.out of JIRA but I want the user to stop making such change (psd < srsd) in case he tries to something like that. 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.project.Project
import com.atlassian.jira.workflow.WorkflowException


public class Pdrtst_test extends AbstractIssueEventListener {
	@Override
	void workflowEvent(IssueEvent event) {
		this.customEvent(event)
	}
	@Override
	void customEvent(IssueEvent event) {
		Issue issue = event.issue 

	ComponentManager componentManager = ComponentManager.getInstance()
	def customFieldManager = componentManager.getCustomFieldManager()

	def cf1 = customFieldManager.getCustomFieldObjectByName("Prod Scheduled Date")
	def psd = issue.getCustomFieldValue(cf1)

	def cf2 = customFieldManager.getCustomFieldObjectByName("SIS Tech Review Start Date")
	def srsd = issue.getCustomFieldValue(cf2)

	if (psd &lt; srsd){
		invalidInputException = new InvalidInputException("customfield_id_10491","Prod Sch date should be greater than SIS date")
		}
	}
}

I have put this script in script-listener and have set its Event as All events.

How can i achieve this ?

I want to show error on JIRA UI and stop user from making such change .

2 answers

1 accepted

5 votes
Answer accepted
Jeremy Gaudet
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.
November 26, 2015

A listener can't generate an error in the UI, as it doesn't come into play until after the triggering event is fired (which means the user has already successfully completed their edit in the UI such that the "Issue Updated" event is fired, for example). Using a listener, you could email the user and tell them they did something wrong; otherwise, if the change is taking place on a workflow transition, you can add a validator. If not, then perhaps the behaviors plugin can be used to add a validation to the field, but I've never done that so don't have any samples. You could restrict editing the field to a workflow transition, even if one that returns to the same state, as a worst case; from there, you remove the field from the general edit screen, and only include it on a screen presented during that transition.

1 vote
Jeremy Gaudet
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.
November 25, 2015

Assuming that is a workflow validator, you don't throw an exception, you use:

import com.opensymphony.workflow.InvalidInputException

invalidInputException = new InvalidInputException("Error here ===============");

 


Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 25, 2015

If it's not a validator, you'll need to explain where you are trying to run this code. Also, how - I'd guess at script-runner, but would need that confirmed.

shashank vivek November 26, 2015

I have added some more info in my code.Please have a look. Its in Script-listner

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 26, 2015

Yes, a listener is the wrong place to do this. I'd like to mark Jeremy's comment here as an answer so you can mark it correct - you need to rethink your approach and go with one of his suggestions.

shashank vivek November 29, 2015

Thanks! Will do some more searching on google and will update this post :)

shivamdu November 30, 2018

@shashank vivek Did you find any?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events