Forums

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

I want to update Labels field with other custom filed value

sysadmin
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 9, 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
Trudy Claspill
Community Champion
September 9, 2022

Hello @sysadmin 

Welcome to the community.

What type of field is sourceFieldName?

sysadmin
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 9, 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
Product Admin
TAGS
AUG Leaders

Atlassian Community Events