Missed Team ’24? Catch up on announcements here.

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

script runner cannot invoke method get value () on a null object

Patricia Pattin October 3, 2022

 

 

Getting this error (see attachment) when trying to run this script (see below). Can anyone advise. 

 

import com.onresolve.scriptrunner.runner.customisers.PluginModule;
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
@WithPlugin("com.atlassian.teams")
@PluginModule GeneralTeamService teamService;

import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.jql.parser.JqlQueryParser;
import com.atlassian.jira.web.bean.PagerFilter;
import com.atlassian.jira.issue.index.IssueIndexingService;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.util.ImportUtils;
import com.atlassian.rm.teams.api.team.GeneralTeamService;
import com.atlassian.rm.teams.core.team.data.DefaultTeam;

import org.apache.log4j.Logger
import org.apache.log4j.Level

// Set-up the logging.
def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

// Set this variable to enable/disable dry run.
def DRY_RUN = false

// Update the filter of issues that we want to update.
def queryString = "(project = NR OR project = NEWRELIC) AND Team = 166";

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser);
def searchService = ComponentAccessor.getComponent(SearchService);
def optionsManager = ComponentAccessor.getComponent(OptionsManager);
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def issueManager = ComponentAccessor.issueManager;

def reIndexIssue(Issue issue) {
    boolean wasIndexing = ImportUtils.isIndexIssues();
    ImportUtils.setIndexIssues(true);
    def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService);
    issueIndexingService.reIndex(issue);
    ImportUtils.setIndexIssues(wasIndexing);
}

def teamField = customFieldManager.getCustomFieldObjectsByName("Team").first();
def gmField = customFieldManager.getCustomFieldObjectsByName("GM Group").first();

def query = jqlQueryParser.parseQuery(queryString);

def search = searchService.search(currentUser, query, PagerFilter.getUnlimitedFilter());

def count = 0

search.results.each{documentIssue ->
    def issue = issueManager.getIssueObject(documentIssue.id);

    // Get team value for this ticket.
    def teamValue = issue.getCustomFieldValue(teamField)
    if (!teamValue) {
        return;
    }
    def teamName = teamValue.getDescription().getTitle()

    // Extract the GM group implied in the name `GM -> Team`
    def splits = teamName.split(" -> ")
    if (splits.size() < 2) {
        return;
    }
    def inferredGmGroup = splits[0]

    def currentGmGroup = issue.getCustomFieldValue(gmField).getValue()

    if (currentGmGroup.equals(inferredGmGroup)) {
        // Ticket is already correct.
        return;
    }
   
    count++
   
    // Update the GM Group field in the ticket
    if (!DRY_RUN) {
        def gmConfig = gmField.getRelevantConfig(issue)
        def gmOptions = optionsManager.getOptions(gmConfig)
        issue.setCustomFieldValue(gmField, gmOptions.getOptionForValue(inferredGmGroup, null))
        ComponentAccessor.issueManager.updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
        reIndexIssue(issue)
    }
}

log.debug("Issues updated: " + count)image.png

1 answer

Suggest an answer

Log in or Sign up to answer
1 vote
Nic Brough -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.
October 3, 2022

There are two things it might be - 

getValue is not a valid call to make on the object being returned by the previous function (which is getCustomFieldValue)

The value in the field is null (the field is empty)

TAGS
AUG Leaders

Atlassian Community Events