Hi Guys,
I'm new to ScriptRunner and want to check out the product. However, when I try to activate scripted field examples, I have a few problems.
The first comes from static type checking:
What I understand from this message is that the fourth argument is not declared or something like that but issue is defined just below. For the moment I can't find the solution, there are a lot of docs on the server and the docs on the Cloud don't help me or I've missed something (it's very likely).
The second, perhaps related to the first, is that I can't activate the field (I can't save it).
Could someone tell me what I've done wrong?
Thank you very much and have a nice day.
// Fetch the issue object from the key using the Jira Software API
def issue = get("/rest/agile/1.0/issue/${issue.key}")
.header('Content-Type', 'application/json')
.asObject(Map)
.body
// Check if the issue is in a sprint and if so if the sprint is an active sprint
if (issue.fields.sprint && issue.fields.sprint.state == 'active') {
// Get the sprint start and end date strings from the API
String sprintStartDate = issue.fields.sprint.startDate
String sprintEndDate = issue.fields.sprint.endDate
// Format the date strings into a nicer format
String formattedSprintStartDate = sprintStartDate.substring(0, 10)
String formatterSprintEndDate = sprintEndDate.substring(0, 10)
// Set the dates in the scripted field
return "Sprint Start Date: ${formattedSprintStartDate} Sprint End Date: ${formatterSprintEndDate}"
// Return a default message if the issue is not in active sprint
} else {
return "The ${issue.key} issue is not currently in an active sprint"
}
With regards to status type checking errors, those are not always indications of bad code.
It's a failure of the editor/syntax checker to extrapolate and be able to guarantee that your code is 100% valid.
In this case
issue.fields.sprint.state
It definitely knows what "issue" is and knows that "fields" is a valid attribute/method for "issue".
It might even know that fields can contain arbitrary data, but that's as far as it goes.
If you use issue.fields.summary, that's all there is, the summary is the final value.
So since the syntax checking is not sure what "sprint" is and whether it's more like summary or has other sub-fields. So it can't verify that "state" is a valid attribute for sprint.
I'm not super familiar with the Cloud implementation of Scriptrunner, but if the status type-checking error bothers you, you can try something like this:
// Check if the issue is in a sprint and if so if the sprint is an active sprint
def sprint = issue.fields.sprint as Map
if (sprint && sprint.state == 'active') {
The "as Map" will tell the static type checking that issue.fields.sprint is a Map that should contain additional sub-fields. So subsequent call to sprint.state will be allowed.
Tanks for your answer, your idea works well for the static checking.
I understand the compiler logic better. Thanks for the tip !
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@JM Perrot Welcome to ScriptRunner!
Your second issue is common as I experience the same thing when I first created a Scripted Field. You have to first test your script against an issue. You'll need to type for issues to appear. Start with the project key. One you test it then you will be able to save it. That may solve your first problem as I am able to run this example script fine. Let me know if this works for you.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Brandon Chin,
Thank you for your reply. This trick for saving work works, thank you! I was stupid, I never tried to type, I was just concentrating on my first problem. ^^
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@JM Perrot happy to help!
Given that you are new to ScriptRunner, if you are interested in a Demo feel free to reach out to customersuccess@scriptrunnerhq.com and address it to myself and I would be happy to go through all the features with you!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.