Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Variable "issue" masks a binding of the same name. Please choose a different name for the variable.

I'm getting this warning (see screenshot) even though my script works perfectly fine.  

variable issue is undelcared.png

 

I've read through the forums as many people have posted this error but no answers seem to make any sense.  Anyone have any practical advice to fix this error for scripts stored like this one?

 

I'm guessing I have some kind of syntactical error but trying to modify my script to look like others here isn't working.

 

This script is used for a script field.

 

2 answers

1 accepted

0 votes
Answer accepted

I think this is somewhat expected.

This script doesn't exist in a vacuum. If you tried to run in the console it would not work because it assumes a certain binding for the context that it will be run in to exist.

For example, if this is a post-function script, when you go to the configuration where that file is called, the static type checking should report green (no errors) because in that context, the issue variable will be declared and the static type checking known that. 
In the Script Editor, the static type checking doesn't know where you intend to use that script and what bindings will be available.

Adding Issue issue= issue in a script is really just a hack/trick to hide ALL OTHER static type checking you would otherwise see in the script editor.

You have to accept this error and move on. Any workaround to get rid of the errors will bring more headache or code complexity than they're worth.
For the record, this should work (I've never actually tried to use this in prod):

//You want to use a different variable than 'issue', otherwise you may get an error that 'issue is already declared'
//and obviously, all subsequent calles to issue should be replaced with new variable name
Issue myIssue = this.binding.variables['issue']

You can read more about Static Type checking here: https://docs.adaptavist.com/sr4js/6.30.2/best-practices/write-code/static-type-checking

My recommendation is to ignore the error. If that's the only one you see in your script, it's in better shape than the majority of my scripts in my environment (>180 scripts).

You see that errors in the Script editor ... but I suspect if you were to put the exact same code n the Script Field in-line script box, you would see this different error instead:

2021-07-21 15_08_17-Window.png

To avoid that error in in-line script, you could remove the line altogether or set it to this:

2021-07-21 15_12_42-Window.png

But this would show the same error in the script editor since script editor doesn't know about "issue".

In the script, "this" can be used to access the class that contains the script, and one of the built-in properties of a script class is its binding which contains the variables defined prior to calling the script.

There is not a single line that you can replace your line 14 that will just work any of the lines I suggest will result in either an error in the Script File Config (context-aware) or in the Script Editor (not context-aware).

The only solution is to NOT attempt to re-use the existing "issue" variable and instead declare a new variable with a different name and loading it with the "this.binding.variables['issue']"

Yes thank you Peter.  It's the only warning and the script runs fine so I've learned to live with it.   It's interesting to learn how the STC works different in the editor versus console versus in-line script.  Thanks for the thorough explanation.

You're welcome.

Might I suggest you change the accepted answer to the answer I provided instead of your reply that was posted as a new response?

Good catch, answer accepted changed. :)

Thank you for the response Peter.  I think I'm following what you are saying.

 

To try your code out in our UAT environment would I literally write this?

Issue myIssue = this.binding.variables['issue']

 aka I  would replace my line with the above line as is?

I'm not sure what you mean 'this.binding.variables['issue'] part.  I don't understand if I'm supposed to put something else there other than what you literally wrote.

Perhaps easier said is, if you were to look at my screenshot and were to replace line 14. how exactly would you replace that line?

Ideally something I could copy and paste exactly.

 

The code is for a script field which has its context set for various issue types and different projects.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

👁‍🗨 Confluence Team planning dashboard_Atlympics 🌈

Hi Atlassian's, How is your journey with #Atlympics 2021 so far....excited! Me too, same excitement. Here's my typical team planning and vision dashboard which I used to share to my management and ...

93 views 1 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you