Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to set custom field value to null in ScriptRunner listener

Vimalraj
Contributor
July 23, 2024

Folks,

I'm trying to set a custom field based on the link type. I'm able to set the linked key through listener. However, I'm unable to clear it when the link is removed. Below is the code snippet. Please assist.

if (it.type.inward == 'is a defect of'){

logger.info("Inward is a defect of links: "+ it.inwardIssue.key)

def result = put("/rest/api/3/issue/${issue.key}") .header('Content-Type', 'application/json')

.body([

fields: [ customfield_14268:it.inwardIssue.key ] ]) .asString()

}else if(it.type.inward == null){

def result = put("/rest/api/3/issue/${issue.key}") .header('Content-Type', 'application/json') .body([ fields: [ customfield_14281: '' ] ]) .asString()

}

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 24, 2024

Hi Vimalraj,

For your requirement you can use an approach similar to what is provided in this Adaptavist code snippet.

I've modified the code snippet above slightly to the working sample below:-

def projectKey = 'MOCK'

def issueType = get('/rest/api/2/issuetype').asObject(List).body.find { it['name'] in ['Story','Task'] }['name']

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

def sampleList = customFields.find { it.id == 'customfield_12818' }?.id

if (issueType in ['Story', 'Task']) {

put("/rest/api/2/issue/${issue.key}")
.header('Content-Type', 'application/json')
.body([
fields: [
(sampleList):null
]
]).asString()
}

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to modify it accordingly.

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

 

 

 

Suggest an answer

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

Atlassian Community Events