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
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
In my case was similar and your solution work for me, thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you very much, this works perfectly
Regards
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ram,
I have used the code suggested by you :
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ram Kumar Aravindakshan _Adaptavist_ ,
I have used the code suggested by you :
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.