How can I fix the error "Cannot get property 'Id' on null object"

98 Views Asked by At

In ScriptRunner Listener for Jira, I got this error....

2023-11-27 08:52:23.844 INFO - Serializing object into 'interface java.util.List' 2023-11-27 08:52:23.915 INFO - GET /rest/api/2/field asObject Request Duration: 955ms 2023-11-27 08:52:24.116 ERROR - Cannot get property 'accountId' on null object on line 25 2023-11-27 08:52:24.120 ERROR - Class: com.adaptavist.sr.cloud.events.WebhookExecution, Config: null

And my script looks like this....

def customFields = get("/rest/api/2/field")
        .asObject(List)
        .body
        .findAll { (it as Map).custom } as List<Map>

//to check the listener is only applied to the correct project and issue type
def projectKey = "SIG" 
if (issue == null || ((Map) issue.fields.project).key != projectKey || issue.fields.issuetype.name != 'Service Request with Approvals') {
    logger.info("Wrong Project ${issue.fields.project.key} or not a SR with Approvals")
    return
}

def ADManagerCfId = customFields.find { it.name == 'AD Manager' }?.id
def ApproversCfId = customFields.find { it.name == 'Approvers' }?.id

//get the values from your issues
def ADManager = issue.fields[ADManagerCfId] as Map
def ApproversName = issue.fields[ApproversCfId] as Map

//update the issue with the new approver
put("/rest/api/2/issue/${issue.key}")
   .header("Content-Type", "application/json")
   .body([
           fields: [
                   "${ApproversCfId}": [ //You need here to interpolate ApproversCfId as a String
                       ['accountId': ADManager.accountId]
                       ]
           ]
   ])
   .asString()
   logger.info ("Updated Approver with ${ADManager.accountId}")

The expectation is... Upon creation of issue record in Jira, the script is suppose to copy the value of AD Manager field to
Approvers field.

What am I doing wrong? And How can I fix the error?

Regards,

1

There are 1 best solutions below

1
On BEST ANSWER

The important error line is:

Cannot get property 'accountId' on null object on line 25

meaning that in line 25 in your code, which looks like to be that one:

                       ['accountId': ADManager.accountId]

The error message states that it can't access / get the property with the name accountId from the variable, as it is null (no value stored in that variable).

So, we need to check where and why the ADManager variable can become null.

The variable is getting introduced in that line:

def ADManager = issue.fields[ADManagerCfId] as Map

assuming that ADManagerCfId is having a correct value, I see just the scenario of that the field is not set at all on that issue. Resulting in getting a null value from issue.fields[ADManagerCfId] and storing it in the ADManager variable.

Let's check the depending ADManagerCfId variable and if it can get a wrong value. The variable is introduced in that line:

def ADManagerCfId = customFields.find { it.name == 'AD Manager' }?.id

For this one, I see just the scenario that it can not find the customField with the specified name AD Manager. You might want to double check if that is the correct name of the field. Furthermore, I recommend to not work with the field names but rather directly with the field ids. When someone renames the field, it will change the field name, but not the field id. For scripts it is therefore safer to use the field id.

By directly using the field id you also end up saving yourself a request call and with that saving runtime. So, double recommendation to use the field id instead of the field name. ;)

That's all what I can see and say with the information provided. :)