Access issue fields during Create

Jozef Vandenmooter July 8, 2020

I'm trying to write a ScriptRunner Validator script that throws an error when the user did not link the issue (Defect) to a parent (Story) during the Create action.

I can't find a way to access the fields of the issue because at that point it's not stored in the database yet. How do I do that?

I know it can be done using a Behaviour, but in this case I'd like to use a Validator.

 

2 answers

1 accepted

2 votes
Answer accepted
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 9, 2020
Jozef Vandenmooter July 9, 2020

Hi @Vikrant Yadav thanks - I actually did look at that one, but I couldn't get it to work because one of the methods gives me an error:

Capture.PNG

Any idea how to fix that?

This is on Jira 8.7.1 and SR 6.2.1

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 9, 2020

@Jozef Vandenmooter have you tried this one also :-

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.fields.IssueLinksSystemField
import com.opensymphony.workflow.InvalidInputException
import webwork.action.ActionContext
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.link.IssueLink


MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("")

def fieldManager = ComponentAccessor.getFieldManager()
def linksSystemField = fieldManager.getField("issuelinks") as IssueLinksSystemField

def request = ActionContext.getRequest()

if (request) {
def params = request.getParameterMap()
def issueLinkingValue = linksSystemField.getRelevantParams(params) as IssueLinksSystemField.IssueLinkingValue

for ( link in issueLinkingValue.getLinkedIssues() ){
def object=ComponentAccessor.getIssueManager().getIssueObject(link)
if(object.getIssueType().toString() != 'Initiative'){
throw new InvalidInputException(IssueFieldConstants.ISSUE_LINKS,
"Note: You must link this Deployment Request to a Initiative.")
}
}
}
Jozef Vandenmooter July 9, 2020

@Vikrant Yadav, that one gives me the same error. Also, there's no need to define the issue, right? It gives me a warning issue is already a binding variable:

Capture.PNG

Jozef Vandenmooter July 10, 2020

That code actually comes straight off of Adaptavist's Examples page:

https://scriptrunner.adaptavist.com/5.5.7/jira/recipes/workflow/validators/validate-attachments-links-in-transition.html

I guess the API was changed since it was written.

Capture.PNG

Like Vikrant Yadav likes this
Jozef Vandenmooter July 17, 2020

Turns out this is not a real error. It's incorrectly reported by the Static Type Checker as an error and can be ignored. The code works fine.

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 17, 2020

@Jozef Vandenmooter  Great, finally you get the working code. :)

I tested on my side also code was working even after giving error. 

maxim.pavlov
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 26, 2022

Hi from the future
Use

requestMap = ActionContext.getRequest()?.getParameterMap()
def blockingIsSet =
requestMap?.get('issuelinks-linktype') == ['blocks'] &&
requestMap?.get('issuelinks-issues')?.size() > 0

instead

No need to import a bunch of unrelated stuff

Like # people like this
Azfar Masut
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 26, 2022

To also add, you may use the following :

if (object.getIssueType().name.toString() != 'Bug')

Following are the full object you can utilize:

IssueConstantImpl[[GenericEntity:IssueType][sequence,null][name,Bug][iconurl,/secure/viewavatar?size=xsmall&avatarId=93577&avatarType=issuetype][description,null][style,null][id,31500][avatar,93577]]

Like Vikrant Yadav likes this
0 votes
Ivana Srebrenova October 20, 2023

Ca be this achieved on Jira Cloud?

Thank you

Suggest an answer

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

Atlassian Community Events