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

JiraUtils.loadComponent method is no longer supported after Jira upgrades to 9.4.3

LipsaPatra March 9, 2023

Hi Team,

 

With Jira upgrade to 9, JiraUtils.loadComponent method is not supported

We are currently facing the issue with JiraUtils.loadComponent method. Its no longer supported. Kindly help us with the compatible method.

Below is the code used:

import com.opensymphony.workflow.WorkflowContext;
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.issue.search.SearchQuery

String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller()
WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class)
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
Long results = null;
CustomField parentLink = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Parent Link").getAt(0)
String parentKey = issue.getCustomFieldValue(parentLink)
IssueManager issueManager = ComponentAccessor.getIssueManager()
MutableIssue parent = issueManager.getIssueByKeyIgnoreCase(parentKey)

def isDevBacklogStatus = parent.getStatus().getSimpleStatus().getName() in ['In Progress']
if (isDevBacklogStatus) {
def jqlSearch = jqlQueryParser.parseQuery("'Parent Link'= " + parentKey + " AND statusCategory not in (Done) AND key != " + issue.getKey());
def searchQuery = SearchQuery.create(jqlSearch, user)
results = searchProvider.getHitCount(searchQuery)
}
if (results == 0) {
workflowTransitionUtil.setIssue(parent)
workflowTransitionUtil.setUserkey(currentUser)
workflowTransitionUtil.setAction(21)
if (workflowTransitionUtil.validate()) {
workflowTransitionUtil.progress()
}
}

Highlighted code gives error during execution. Kindly help us with the correct method.

Waiting for a quick reply.

Thanks

Lipsa

5 answers

Suggest an answer

Log in or Sign up to answer
2 votes
Benjamin S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 11, 2023

Hi folks,

We can't use ComponentAccessor.getComponent directly here since WorkflowTransitionUtil isn't a singleton provided by Jira's component registrar. Since WorkflowTransitionUtil is mutable (setIssue(), setParams(), etc.), it couldn't possibly be a singleton.

Instead, we should use WorkflowTransitionUtilFactory's create() method to get a new instance of WorkflowTransitionUtil.

Here's an example:

import com.atlassian.jira.workflow.WorkflowTransitionUtilFactory
import com.atlassian.jira.workflow.WorkflowTransitionUtil
import com.atlassian.jira.component.ComponentAccessor

WorkflowTransitionUtilFactory wfTransitionUtilFactory = ComponentAccessor.getComponent(WorkflowTransitionUtilFactory)
WorkflowTransitionUtil wfTransitionUtil = wfTransitionUtilFactory.create()

As a side note, it's bad programming practice to directly reference the implementation (that is, WorkflowTransitionUtilImpl). Instead, we should target its interface WorkflowTransitionUtil.

I hope this helps! Please drop me a reply if I can clarify anything.

Warm regards,
Ben | Jira Server/DC Support

Adam Roszkiewicz
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!
September 8, 2023

In my case was similar and your solution work for me, thanks

Oğuz Kaan Baysal
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!
November 13, 2023

Thank you very much, this works perfectly

Regards

1 vote
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 9, 2023

Hi @LipsaPatra

Since the JiraUtils.loadComponent has been deprecated and removed in Jira 9 why not just use the ComponentAccessor to invoke the class? 

So instead of using:-

WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class)

Just use:

import com.atlassian.jira.component.ComponentAccessor


def workflowTransitionUtil = ComponentAccessor.getComponent(WorkflowTransitionUtilImpl) as WorkflowTransitionUtil

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

LipsaPatra March 9, 2023

Hi Ram,

Thanks for the prompt reply. However while the above codes gives null value for the workflowTransitionUtil.

However, while hovering over the .loadComponent, it suggests to use ComponentManager.loadComponent(Class, Collection)

 

Could you please me know how would i modify the code based on the above suggestion.

Also help us with import statement applicable for the loadComponent method

Regards

Lipsa

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 9, 2023

Hi @LipsaPatra

Using the ComponentManager is not recommended, as it has been deprecated since Jira 7.11 and is no longer supported.

You need to use the ComponentAccessor.getComponent approach.

Below is a sample code for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.workflow.WorkflowTransitionUtil

def workflowTransitionUtil = ComponentAccessor.getComponent(WorkflowTransitionUtil)
workflowTransitionUtil.setIssue(issue)

I don't seem to have any issues invoking the WorkflowTransitionUtil using the above approach.

Thank you and Kind regards,

Ram

LipsaPatra March 13, 2023

Hi Ram,

I have used the code suggested by you :


import com.opensymphony.workflow.WorkflowContext;
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
 
String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller()
def workflowTransitionUtil = ComponentAccessor.getComponent(WorkflowTransitionUtilImpl) as WorkflowTransitionUtil
log.info(workflowTransitionUtil)
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField epicLink = customFieldManager.getCustomFieldObjectByName('Epic Link');
//CustomField epicLink = customFieldManager.getCustomFieldObjectByName('Epic Link')
MutableIssue parent = (MutableIssue) issue.getCustomFieldValue(epicLink);
if (parent != null) {
    log.warn "${parent.getSummary()}"
   
    log.warn "${parent.getStatus().getSimpleStatus().getName()}"

    def isDevBacklogStatus = parent.getStatus().getSimpleStatus().getName() in ['New']
    log.warn "${isDevBacklogStatus}"
    log.info(parent)

    if (isDevBacklogStatus) {
        workflowTransitionUtil.setIssue(parent)
        workflowTransitionUtil.setUserkey(currentUser)
        workflowTransitionUtil.setAction(11)
        if (workflowTransitionUtil.validate()) {
            log.warn "inside validate"
            workflowTransitionUtil.progress()
        }        
    }
}
But still I am unable to get the desired results. I have added the piece of code entire piece of code. Let me where I am going wrong.
I am getting java.lang.NullPointerException: Cannot invoke method setIssue() on null object at Script152.run(Script152.groovy:31)
Regards
Lipsa
LipsaPatra March 13, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

I have used the code suggested by you :


importcom.opensymphony.workflow.WorkflowContext;
importcom.atlassian.jira.workflow.WorkflowTransitionUtil;
importcom.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;
importcom.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.Issue;
importcom.atlassian.jira.issue.fields.CustomField;
importcom.atlassian.jira.component.ComponentAccessor;
 
String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller()
def workflowTransitionUtil = ComponentAccessor.getComponent(WorkflowTransitionUtilImpl) as WorkflowTransitionUtil
log.info(workflowTransitionUtil)
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField epicLink = customFieldManager.getCustomFieldObjectByName('Epic Link');
//CustomField epicLink = customFieldManager.getCustomFieldObjectByName('Epic Link')
MutableIssue parent = (MutableIssue) issue.getCustomFieldValue(epicLink);
if (parent != null) {
    log.warn "${parent.getSummary()}"
   
    log.warn "${parent.getStatus().getSimpleStatus().getName()}"

    def isDevBacklogStatus = parent.getStatus().getSimpleStatus().getName() in ['New']
    log.warn "${isDevBacklogStatus}"
    log.info(parent)

    if (isDevBacklogStatus) {
        workflowTransitionUtil.setIssue(parent)
        workflowTransitionUtil.setUserkey(currentUser)
        workflowTransitionUtil.setAction(11)
        if (workflowTransitionUtil.validate()) {
            log.warn "inside validate"
            workflowTransitionUtil.progress()
        }        
    }
}
But still I am unable to get the desired results. I have added the piece of code entire piece of code. Let me where I am going wrong.
I am getting java.lang.NullPointerException: Cannot invoke method setIssue() on null object at Script152.run(Script152.groovy:31)
Regards
Lipsa
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 4, 2023

Hi @LipsaPatra

If you go through the latest code that you have shared, you are using:-

String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller()

def workflowTransitionUtil = ComponentAccessor.getComponent(WorkflowTransitionUtilImpl) as WorkflowTransitionUtil

In my last comment, I have already suggested invoking the WorkflowTransitoinUtil using:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.workflow.WorkflowTransitionUtil

def workflowTransitionUtil = ComponentAccessor.getComponent(WorkflowTransitionUtil)
workflowTransitionUtil.setIssue(issue)

This doesn't appear to have been changed in your code.

And upon looking at the line where the exception is being thrown, i.e. line 31:-

if (workflowTransitionUtil.validate()) {

It clearly shows that it doesn't recognize the workflowTransitionUtil object resulting in the validation to fail. 

Please make the modification that I suggested earlier and retest it once again.

Thank you and Kind regards,

Ram

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 4, 2023

Hi @LipsaPatra

The ComponentManager has been Deprecated since Jira 7.11.0.

Please do not use this to invoke the WorkflowTransitionUtil. You must use the ComponentAccessor else your code will have problems.

Thank you and Kind regards,
Ram

0 votes
Jyoti Verma August 4, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

Could you please suggest - I tried above solution and getting error

java.lang.NullPointerException: Cannot invoke method setIssue() on null object at Script343.run(Script343.groovy:74)

0 votes
Jyoti Verma July 31, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ @LipsaPatra 

I am also getting the same error - 

Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script> java.lang.NullPointerException: Cannot invoke method setIssue() on null object

Any idea?

Regards

Jyoti

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 4, 2023

Hi @Jyoti Verma

Could you please share the full code that you are using so I can review it and provide some feedback?

Thank you and Kind regards,
Ram

Jyoti Verma August 4, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

Here is the code:

 

import com.atlassian.jira.component.ComponentAccessor

import com.opensymphony.workflow.WorkflowContext

import com.atlassian.jira.workflow.WorkflowTransitionUtil

import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl

import com.atlassian.jira.util.JiraUtils

import com.atlassian.jira.workflow.JiraWorkflow

import com.atlassian.jira.user.ApplicationUser

import com.atlassian.jira.issue.MutableIssue

import com.atlassian.jira.issue.link.IssueLinkManager

import com.atlassian.jira.issue.link.IssueLink

import com.opensymphony.workflow.spi.Step

import com.atlassian.jira.issue.status.Status

import com.atlassian.jira.issue.ModifiedValue

import com.atlassian.jira.event.type.EventDispatchOption

import com.atlassian.jira.issue.index.IssueIndexManager

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import com.atlassian.jira.issue.util.IssueChangeHolder

import com.atlassian.jira.workflow.TransitionOptions

import com.atlassian.jira.bc.issue.IssueService

import com.atlassian.jira.util.ErrorCollection

IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()

MutableIssue mutIssue = issue

List outwardlinks = issueLinkManager.getOutwardLinks(mutIssue.getId())

def customFieldManager = ComponentAccessor.customFieldManager

String status = mutIssue.getStatus().getName()

    for(IssueLink outwardlink : outwardlinks)

    {

        MutableIssue linkedOutDestIssue= (MutableIssue)outwardlink.getDestinationObject()

        String outwardDesc = outwardlink.getIssueLinkType().getOutward()

        String linkIssStatus = linkedOutDestIssue.getStatus().getName()

       

        //copy component, fix version, resolution from master

        Collection components = mutIssue.getComponents()

                   

        linkedOutDestIssue.setResolution(mutIssue.getResolution())

       

       

       

        int actionID = 0

       

        if (outwardDesc == "is master of") {  

            if (linkIssStatus != status) {

                IssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder();

                def cf_RD = customFieldManager.getCustomFieldObjectByName("Resolution Description")    

                 ModifiedValue modifiedValue = new ModifiedValue(linkedOutDestIssue.getCustomFieldValue(cf_RD), mutIssue.getCustomFieldValue(cf_RD))                        

                linkedOutDestIssue.setCustomFieldValue(cf_RD, mutIssue.getCustomFieldValue(cf_RD))

                cf_RD.updateValue(null, linkedOutDestIssue, modifiedValue, issueChangeHolder)

       

                def cf_PR = customFieldManager.getCustomFieldObjectByName("Pending reason")

                 modifiedValue = new ModifiedValue(linkedOutDestIssue.getCustomFieldValue(cf_PR), mutIssue.getCustomFieldValue(cf_PR))                        

                linkedOutDestIssue.setCustomFieldValue(cf_PR, mutIssue.getCustomFieldValue(cf_PR))

                cf_PR.updateValue(null, linkedOutDestIssue, modifiedValue, issueChangeHolder)

                issueChangeHolder = new DefaultIssueChangeHolder()

                linkedOutDestIssue.setCustomFieldValue(customFieldManager.getCustomFieldObjectByName("hiddenTransition"), "Allow Transition")

                modifiedValue = new ModifiedValue(linkedOutDestIssue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("hiddenTransition")), "Allow Transition")                                                

                customFieldManager.getCustomFieldObjectByName("hiddenTransition").updateValue(null, linkedOutDestIssue, modifiedValue, issueChangeHolder)

                ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

                ComponentAccessor.getIssueManager().updateIssue(user, linkedOutDestIssue, com.atlassian.jira.event.type.EventDispatchOption.DO_NOT_DISPATCH, false)

               

                //Dup Pending (1151)

                //PENDING  

                if(status.equalsIgnoreCase("Pending")){

                    actionID = 1151

                }

           

    String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller()

    //WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class)

    def workflowTransitionUtil = ComponentAccessor.getComponent(WorkflowTransitionUtilImpl) as WorkflowTransitionUtil

    workflowTransitionUtil.setIssue(linkedOutDestIssue)

    workflowTransitionUtil.setUserkey(currentUser)

    workflowTransitionUtil.setAction(actionID)

    if (workflowTransitionUtil.validate()) {

        ErrorCollection error = workflowTransitionUtil.progress()

        System.out.println "++++error++++++" + error

    }

            }//linkIssStatus != status

            linkedOutDestIssue.store()

        }//(outwardDesc == "is master of"

    }//outwardlink
0 votes
LipsaPatra March 9, 2023

Hi Ram,

Thanks for the prompt reply. However while the above codes gives null value for the workflowTransitionUtil.

However, while hovering over the .loadComponent, it suggests to use ComponentManager.loadComponent(Class, Collection)

 

Could you please me know how would i modify the code based on the above suggestion.

Also help us with import statement applicable.

Regards

Lipsa

TAGS
AUG Leaders

Atlassian Community Events