Getting null pointer exception while setting a custom field in groovy script

Hi,

I am trying to write this simple script that sets the value of a custom field depending on a value of a different custom field

This is the code

import com.atlassian.jira.issue.IssueManager

import com.atlassian.jira.ComponentManager

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.customfields.CustomFieldType

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.issue.MutableIssue

import com.atlassian.jira.project.Project

import com.atlassian.jira.project.ProjectManager

import com.atlassian.jira.issue.issuetype.IssueType

import com.atlassian.jira.issue.IssueImpl





IssueManager issueManager = ComponentManager.getInstance().getIssueManager();

CustomFieldManager cfManager = ComponentManager.getInstance().getCustomFieldManager();

ProjectManager projectManager = ComponentManager.getInstance().getProjectManager();



MutableIssue myissue = issueManager.getIssueObject("POA-6");



CustomField cfAPType = cfManager.getCustomFieldObjectByName("AP type");

CustomField cfPOType = cfManager.getCustomFieldObjectByName("PO type");

CustomField cfProposedDeveloper = cfManager.getCustomFieldObjectByName("Proposed Developer");





def currentissuetype=myissue.getIssueType()

def currentissuetypenm=currentissuetype.name

def currentAPType=myissue.getCustomFieldValue(cfAPType)

def currentPOType=myissue.getCustomFieldValue(cfPOType)

def currentdev=myissue.getCustomFieldValue(cfProposedDeveloper)

def developer="pink blue"



if (currentissuetypenm == "Zaratec")

     {

     myissue.setCustomFieldValue(currentdev,developer)

     }

Any idea why I am getting a null pointer exception.

javax.script.ScriptException: java.lang.NullPointerException

Thanks

2 answers

Hi

How do you try to run that script?

And at which line do you get the NullPointerException?

Greetings

Hermann

Hi Shamhoon,

if i got it, you want to set the value of custom field according the issue type. If that's right, code should be something like:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.comments.CommentManager;

  
// gets a reference to the IssueManager and CustomFieldManager manager classes
ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();


//Get the custom field you want to update by id

CustomField customField_toupdate = customFieldManager.getCustomFieldObject( cf_id );




//issue type condition


if (issue.getIssueTypeObject().getName() == "issue_type_name"){

def val_test = "enter_here_the_desired_value"

issue.setCustomFieldValue(customField_toupdate, val_test)
}

Test it first.

If you use script in create transition place your scripted function in top of list. Add also the store issue function.

Hope that helps,

Cheers

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 30, 2018 in Marketplace Apps

Three tips for boosting your board's efficiency with Story Maps

Trello is one of the most effective tools for driving your sprints. It's customizable for every Agile team and product owners and Scrum masters (SM) love it. However, Agile teams often struggle with:...

853 views 2 9
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