Groovy Class Multiple Instances without Duplicating

David Harkins
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.
September 28, 2023

I'm have some issue with a ScriptRunner / Groovy script.

Improving our Incident and Problem process,

Have a defined class with a constructor that takes an issue and populates a variable.

I can create one instance of the class and everything works as expected.

The moment i create a 2nd instance on the same class, the 1st one i created returns the same values as the newly created class.

 

import ASI.Issue_Client
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level
import org.apache.log4j.Logger
def logOutput = Logger.getLogger('CONSOLE')
logOutput.setLevel(Level.DEBUG)
logOutput.info('Script Starting')
def issueManager = ComponentAccessor.getIssueManager()

Issue_Client activeIncidentIssue = new Issue_Client ( issueManager.getIssueObject( 'issue-94002' ) )

logOutput.debug('activeIncidentIssue: ' + activeIncidentIssue.WorkingIssue.key)
logOutput.debug('----------')

Issue_Client activeProblemIssue = new Issue_Client (issueManager.getIssueObject( 'issue-94123' ))

logOutput.debug('activeIncidentIssue: ' + activeIncidentIssue.WorkingIssue.key)
logOutput.debug('activeProblemIssue: ' + activeProblemIssue.WorkingIssue.key)

I get the following log output:

 

2023-09-28 13:11:25,933 INFO [CONSOLE]: Script Starting

2023-09-28 13:11:25,949 DEBUG [CONSOLE]: activeIncidentIssue: issue-94002

2023-09-28 13:11:25,949 DEBUG [CONSOLE]: ----------

2023-09-28 13:11:25,949 DEBUG [CONSOLE]: activeIncidentIssue: issue-94123

2023-09-28 13:11:25,949 DEBUG [CONSOLE]: activeProblemIssue: issue-94123

 

The activeIncidentIssue should remain the same value

 

The key values from my class definition are:

public class Issue_Base {
    public static Issue WorkingIssue
    public Issue_Base (Issue issue) {
        WorkingIssue = issue
    }
}

 

1 answer

1 accepted

0 votes
Answer accepted
Peter-Dave Sheehan
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.
September 28, 2023

Don't make the "WorkingIssue" field in your class "static".

Static means that this is applicable to the blueprint rather than a specific instance.

When it's static, all instances of that class will have the same value and it will change for all instances each time to set the value.

Also, it is against java conventions to have fields start with a capital letter and word separation in class names is normally achieved with camel case rather than underscore.

David Harkins
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.
September 29, 2023

@Peter-Dave Sheehan 

Many Thanks :-)

Conventions are work in progress :-)

Only pasted a snapshot of code, have methods in the class that could not access a non-static variable, because they were static themselves. Not anymore :-D

Everyday is a school day :-)

Suggest an answer

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

Atlassian Community Events