Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,360,199
Community Members
 
Community Events
168
Community Groups

I want to update Labels field with other custom filed value

sysadmin I'm New Here Sep 09, 2022

I built the script below in order to get value from the custom filed "sourceFieldName" then insert it to the custom filed "selectListFieldName" (Labels filed).

 

But unfortunately, it returns always null and the script is not able to return even the "sourceFieldName" value.

 

The script :

def issueKey = 'SS-43'

def issue = get("/rest/api/2/issue/${issueKey}")
.header('Content-Type', 'application/json')
.asObject(Map)
.body

def fields = issue.fields as Map


// Specify the name of the select list field to set
def selectListFieldName = 'customfield_10131'
def sourceFieldName = 'customfield_10116'

//logger.info("${(selectListFieldName.value)}")
//logger.info("${(sourceFieldName.value)}")

// Get the Custom field to get the option value from
def customField = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).name == selectListFieldName
} as Map

def customField2 = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).name == sourceFieldName

} as Map

//logger.info("customField${(customField)}")


\

def value3 = (fields[customField2] as String)?.value

logger.info("${(value3)}")
// logger.info("${(value4)}")


//def input1 = fields[customField2].value as String


// Check if the custom field returns a valid field and is not null
assert customField != null : "Cannot find custom field with name of: ${selectListFieldName}"

// Note: - Atlassian provide examples for the syntax to set other custom field types in the documentation page at https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/#creating-an-issue-examples
def result = put("/rest/api/2/issue/${issueKey}")
// Uncomment the line below if you want to set a field which is not pressent on the screen. Note - If using this you must run the script as the ScriptRunner Add-On User.
//.queryString("overrideScreenSecurity", Boolean.TRUE)
.header('Content-Type', 'application/json')
.body([
fields: [
(customField.id) : "${(value3)}"
]
])
.asString()

if (result.status == 204) {
return "The ${customField.name} select list field was successfully updated on the ${issueKey} issue"
} else {
return "${result.status}: ${result.body}"
}

1 answer

0 votes

Hello @sysadmin 

Welcome to the community.

What type of field is sourceFieldName?

sysadmin I'm New Here Sep 09, 2022

Thank you Trudy

sourceFieldName is a  Number filed

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Site Admin
TAGS
Community showcase
Published in Jira Software

An update on Jira Software customer feedback – June 2022

Hello Atlassian Community! Feedback from customers like you has helped us shape and improve Jira Software. As Head of Product, Jira Software, I wanted to take this opportunity to share an update on...

5,012 views 18 31
Read article

Atlassian Community Events