Unable to resolve issuecontextImpl in a behaviour script

I am trying to integrate IssuecontextImpl in the Behaviour .. groovy Code is working fine without the Jira API but when integrating with Jira API i am getting this compilation error .

 

Highlighted the error line in BOLD.Error :Cannot find matching method HTTPGroovy#getIssueContext

 

import groovy.json.JsonSlurper
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.DefaultOptionsManager
import com.atlassian.jira.project.CachingProjectManager
import com.atlassian.jira.issue.managers.CachingCustomFieldManager
import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.customfields.option.OptionsImpl

class HttpGroovy {

static URL url1, url2
public static int recCount = 0;
public static int totalRec = 0;
List applicationIdDetails = []
public static int offset = 0;

static main(args) {

 

def projectMgr = ComponentAccessor.getProjectManager() as CachingProjectManager;
IssueContextImpl issueContext = getIssueContext() as IssueContextImpl;
def customeFieldMgr = ComponentAccessor.getCustomFieldManager() as CachingCustomFieldManager
def customeField = customeFieldMgr.getCustomFieldObject(customFieldId);
FieldConfig fieldConfig = customeField.getRelevantConfig(issueContext);
def optManager = ComponentAccessor.getOptionsManager() as DefaultOptionsManager;
OptionsImpl optionsImpl = optManager.getOptions(fieldConfig) as OptionsImpl;

List<Option> lists = new ArrayList();
Long opId = 1L;
appDataList.each {
if (optionsImpl.getOptionForValue(it.toString(), null) == null) {
Option option = optManager.createOption(fieldConfig, null, opId, it.toString());
opId += 1L;
lists.add(option);
}

}
if (lists.size() > 0) {
optManager.updateOptions(lists);
}


}

 

Any inputs will be really helpful

 

1 answer

This widget could not be displayed.
Alexey Matveev Community Champion Sep 08, 2018

Hello,

Why do you define a class in your script? Have a look at the examples on how to write a behaviour:

https://scriptrunner.adaptavist.com/latest/jira/behaviours-overview.html

If you want to define a class then you need to extend from FieldBehaviours.

First I wrote the script , but later modified and included a class as I have to  hit the api mutliple times to get all the records and load them to the custom field . When I ran the script alone ..no issues found and later when I had included the Jira API I got this error .

So will extending the class from FieldBehaviours resolves this issues . Sorry I am new to Jira and groovy so having a lot of questions ..

Alexey Matveev Community Champion Sep 10, 2018

Extending your class from FieldBehaviours should resolve your issue.

Error didn't resolved :( 

But instead of below error :

Cannot find matching method HTTPGroovy#getIssueContext

it ended with FieldBehaviours#getIssueContext method not found

Alexey Matveev Community Champion Sep 10, 2018

How did you call the getIssueContext method?

import com.atlassian.jira.issue.context.IssueContextImpl

tried both statements :

IssueContextImpl issueContext = getIssueContext() as IssueContextImpl;


IssueContextImpl issueContext = IssueContextImpl.getIssueContext() as IssueContextImpl;

Alexey Matveev Community Champion Sep 10, 2018

It looks as if you call it in a static method. Do you call it in the main method?

yeah called it in static main method

Alexey Matveev Community Champion Sep 10, 2018

You do not need a static method. When you define a behaviour, you define the entrance method and it is not static. From this method you can instantiate an instance of your class and call your method.

Yeah modified the class and now errors got resolved  . But my values are not populating on the custom field which is a drop down . I do have 2900 records ,is that the issue 

Hi Alexey ,

 

I am able to get the list which contains 2900 records , but when trying to populate to custom field they are not getting populated .Can u suggest do i need to update the code 

def populateCustomField(List carIDAppList){

println carIDAppList.size();//getting the list here

def projectMgr = ComponentAccessor.getProjectManager() as CachingProjectManager;
IssueContextImpl issueContext = getIssueContext() as IssueContextImpl;
//IssueContextImpl issueContext = IssueContextImpl.getIssueContext() as IssueContextImpl;
def customeFieldMgr = ComponentAccessor.getCustomFieldManager() as CachingCustomFieldManager
def customeField = customeFieldMgr.getCustomFieldObject(customFieldId);
FieldConfig fieldConfig = customeField.getRelevantConfig(issueContext);
def optManager = ComponentAccessor.getOptionsManager() as DefaultOptionsManager;
OptionsImpl optionsImpl = optManager.getOptions(fieldConfig) as OptionsImpl;

List<Option> lists = new ArrayList();
Long opId = 1L;
carIDAppList.each {
if (optionsImpl.getOptionForValue(it.toString(), null) == null) {
Option option = optManager.createOption(fieldConfig, null, opId, it.toString());
opId += 1L;
lists.add(option);
}

}
if (lists.size() > 0) {
optManager.updateOptions(lists);
}
}

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

533 views 3 19
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you