"Cannot get property 'accountId' on null object" error message in ScripRunner

Zaldy Parian
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 27, 2023

Hi,

Hope someone can help me with this error. I created a script to copy the value of AD Manager field to Approvers field through ScriptRunner. However, I keep on getting some errors...

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

My script is 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}")

Appreciate your suggestion/guidance on how I could fix this error.

 

Cheers,

1 answer

1 accepted

0 votes
Answer accepted
Kristian Walker _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.
November 27, 2023

Hi Zaldy,

Thank you for your question.

I would advise wrapping your logic in an If statement, which checks if the AD Manager field value is not null and only tries to copy the value if the field is not null.

This error can occur when a field value is null, which is why I would suggest adding this check to the script.

I hope this helps.

Regards,

Kristian

Zaldy Parian
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 28, 2023

Thanks, @Kristian Walker _Adaptavist_ 

I will give your suggestion a go. And get back to you.

Cheers,

Zaldy Parian
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 30, 2023

Hi @Kristian Walker _Adaptavist_ ,

The ScriptL istener is working on create. It automatically copy the AD Manager value to the Approvers field. However I found out that when the same issue record was edited, the ScriptRunner errors out. Which in turn sends an email notification to me. The main body of the email is like this...

Processing webhook event from JIRA Cloud failed, please find details below.

You can find more details about this error on Logs page. 

CorrelationId: Self=1-65694882-1b20438028a6c08a52a1d8bd;Root=1-65694882-563da02f27c6ea5c57a7fe27

Issue ID:  ABC-123

Event type: issue created

Affected scripts:

Description:

SIG: Copy AD Manager to Approvers field

UUID:

d2959473-6a3c-4e3d-a3d5-349311ed02cc

Execution user:

ScriptRunner Add-On User

 

Error: java.lang.RuntimeException: java.lang.NullPointerException: Cannot get property 'accountId' on null object at Script1.run(Script1.groovy:27) at Script1$run.call(Unknown Source) at Script1$run.call(Unknown Source) at com.adaptavist.sr.cloud.workflow.AbstractScript.evaluate(AbstractScript.groovy:36) at com.adaptavist.sr.cloud.events.ScriptExecution.run(ScriptExecution.groovy:29) at WebhookExecution1_groovyProxy.run(Unknown Source)

then followed by the payload.

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

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

//update the issue with the new approver
put("/rest/api/2/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields: [
"${ApproversCfId}": [ // THIS IS LINE 27
['accountId': ADManager.accountId]
]
]
])
.asString()
logger.info ("Updated Approver with ${ADManager.accountId}")

error_log.png

If you noticed, I even added an IF logic that if Approvers != null, do nothing, but it seems it still going through the rest of the script.

Apologies for this as I'm just a beginner in groovy script.

Zaldy Parian
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 30, 2023

Should I add a script in the If the following condition evaluates to true: section of the Listener? If so, what would be the script if I want to check if the cascading customfield_10220 (name: Issue category) was changed?

So that, if the condition is not true, it will not execute the Code to run: .

Kristian Walker _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.
December 4, 2023

Hi Zaldy,

To stop the script from running, you need to add a return statement in the if statement that way if the condition is met it will not run the script.

In your code above you are doing if(AproversName !=null) then return, this means the script will stop when the field has a value and your check should be == null instead.

The page located here explains how If / Else statements work in Groovy and will be a good guide to help understand how these can help control the flow of the code in your script.

I hope this helps.

Regards,

Kristian

Zaldy Parian
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 4, 2023

Hi @Kristian Walker _Adaptavist_ 

Thanks for your reply and suggestion. 
If the replace != with ==; the code will totally not run and will value from AD Manager will not be copied to the Approvers field, on both create and edit events.

What I was hoping is if the Approvers field already have value(s) in it, do not process the rest of the script and just "return".

Sorry if I'm not clear with my previous statements.

 

Cheers,

Kristian Walker _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.
December 5, 2023

Hi Zaldy,

Thank you for your response.

If you add a return statement in your script, I can confirm that it will stop the whole script from running, as this is how the product is designed.

This means you should add the return inside your if statement which checks if the approvers field has a value and then this will stop the whole script at this point.

If you want just to skip some logic if the value is set, then this logic needs to be in the if statement.

Then, this logic will be skipped when the field has a value, and the rest of the script will still be run.

I hope this helps.

Regards,

Kristian

Zaldy Parian
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 5, 2023

Thanks, @Kristian Walker _Adaptavist_ ,

I moved the main action (copying of AD Manager to Approvers field) in the ELSE condition. And I think that did the trick.

Thank you so much for your help.

Kristian Walker _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.
December 6, 2023

Hi Zaldy,

I am glad we managed to assist in getting your script working.

Regards,

Kristian

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events