Hello Team,
I'm trying to write a script to change the description of a field based on another check box field value. But getting some errors in the script. Can someone please check and suggest the solution for this??
def newDNSRequest = getFieldById(getFieldChanged ( ) )
def Host(A Record) = getFieldByName("Host (A Record)")
def Alias(CNAME) = getFieldByName("Alias (CNAME)")
def newDNSRequest = orderRequest.getValue().toString()
if ((getFieldById(fieldChanged).value as "Host (A Record)")?.name) {
getFieldById('Hostname')
.setDescription('No FQDN')
} else {
getFieldById('Hostname')
.setDescription('FQDN Mandatory')
}
if ((getFieldById(fieldChanged).value as "Host (A Record) && Alias (CNAME)")?.name) {
getFieldById('Hostname')
.setDescription('No FQDN')
}
Error in
@ line 2, column 20.
After reviewing your code, I can confirm it will fail 100%.
There are many mistakes in your code.
Firstly there is no point in using getFieldById(getFieldChanged()) in your if/else conditions if you have already declared a variable for it.
Also, in your code, I do not see any usage of the variables, def Host(A Record) = getFieldByName("Host (A Record)") and def Alias(CNAME) = getFieldByName("Alias (CNAME)").
Only the field name is used as a value in the if / else conditions.
You may as well remove these field declarations.
Secondly, it's a bad practice to declare in the if/else condition like
if ((getFieldById(fieldChanged).value as "Host (A Record)")?.name) {
getFieldById('Hostname')
.setDescription('No FQDN')
} else {
getFieldById('Hostname')
.setDescription('FQDN Mandatory')
}
You should declare it outside the if/else condition to avoid any confusion.
Below is a complete sample cleaned-up code for your reference:-
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def newDNSRequest = getFieldById(fieldChanged)
def newDNSRequestValue = newDNSRequest.value.toString()
def hostname = getFieldByName('Hostname')
hostname.description = ''
if (newDNSRequestValue == 'Host (A Record)') {
hostname.description = 'No FQDN'
} else if (newDNSRequestValue in ['Host (A Record)', 'Alias (CNAME)']) {
hostname.description = 'No FQDN'
} else {
hostname.description = 'FQDN Mandatory'
}
I'm not sure if this will work 100%.
It would be best if you could share a screenshot of the fields you want to use and a screenshot of your Behaviour configuration to double-confirm if this will work in your environment.
I would also recommend that you go through the ScriptRunner Behaviour documentation and look at the videos to better understand how you should handle your code.
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
So..
getFieldById(getFieldChanged ( ) )
This I don't know if groovy will swallow but I assume static type checking should be red if it didn't.
In any case, the correct format would be
getFieldById(getFieldChanged())
The second part is totally wrong
def Host(A Record)
Variables cannot be declared with spaces. This will not compile.
The third part has me confused
def newDNSRequest = getFieldById(getFieldChanged ( ) )
def Host(A Record) = getFieldByName("Host (A Record)")
def Alias(CNAME) = getFieldByName("Alias (CNAME)")
def newDNSRequest = orderRequest.getValue().toString()
You're declaring 'newDNSRequest' first, but then you are trying to declare it again 3 lines below it.
Re-declaring is not possible in java, whether groovy allows it, I do not know but I assume so, either way, to set a different value to an existing variable, there should be no "def" anymore.
But what's more important is the fact that it refers to "orderRequest" - but I do not see that anywhere in this script at all, meaning that this would fail to compile altogether as that orderRequest is not defined anywhere.
The fourth part is also funny
(getFieldById(fieldChanged).value as "Host (A Record)")
'as' is used to cast one data type to another. 'Host (A Record)' is not a data type, that's just a random string.
.value will give you the underlying java data type, such as Double, String, or whatever else is being stored in this field, such as custom types from Jira - if you were getting a value from a version-picker, then that value data type would be Version.
You can try to "cast" an Integer to a String, such as 'def newString = 55 as String', which would be the equivalent of 'def newString = "55"', but you have to be using an actually valid data type.
The other thing is, you're again referring to an undefined variable here, 'fieldChanged' is not a method, and it's not a declared variable in your script, so this, again, won't compile.
Static type checking is there for a reason - if you go line by line, static type checking will tell you, if you read it, what the problem is.
This snippet should be screaming red the second you start writing it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Want to make your everyday Community actions directly contribute to reforestation? The Atlassian Community can achieve this goal by liking a post, attending an ACE, sending your peers kudos, and so much more!
Help us plant more trees
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.