Forums

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

Set Asset issue field in post function script

Tom Lister
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 13, 2023

Hi

I need to set an Asset field value for a team in a post function. But I'm struggling to find a syntax to set the value.

Should be the asset id or guid.

where 10010 is the issue custom field

put('/rest/api/2/issue/' + issue.key ) 
.header('Content-Type', 'application/json')
.body(
[
fields: [
issuetype : [
id: taskType
],
"customfield_10010" : "CT-1650"

]
])
.asString().status

1 answer

1 accepted

2 votes
Answer accepted
Ram Kumar Aravindakshan _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 26, 2023

Hi @Tom Lister

For your requirement, you will need to use ScriptRunner's Run Script Post-Function.

Below is a template of the code for your reference:-

def issueKey = issue.key
def workspaceId = '<WORKSPACE_ID>' 
def newObjectId = '<OBJECT_ID>'

def updateMap = ['workspaceId': workspaceId,'id': "${workspaceId}:${newObjectId}", 'objectId': newObjectId]

put("/rest/api/2/issue/${issueKey}")
    .header('Content-Type', 'application/json')
    .body([ 'update': [ '<ASSET_CUSTOM_FIELD_ID>' : [ [ 'set': [ updateMap ] ] ] ] ]).asString().body

Below is an example of a fully working code for your reference:-

def issueKey = issue.key
def workspaceId = '50ac2477-8e26-4836-ae41-22d86f326ee4' 
def newObjectId = '3'

def updateMap = ['workspaceId': workspaceId,'id': "${workspaceId}:${newObjectId}", 'objectId': newObjectId]

put("/rest/api/2/issue/${issueKey}")
    .header('Content-Type', 'application/json')
    .body([ 'update': [ 'customfield_10043' : [ [ 'set': [ updateMap ] ] ] ] ]).asString().body

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below are screenshots of the configuration:-

1. First, Add a post function as shown in the screenshot below:-

config1.png

2. From the options provided, select ScriptRunner Post-Function as shown in the screenshot below:-

config2.png

3. Next, select the Run Script option as shown in the screenshot below:-config3.png

4. Once you are on the Run Script page, add the sample script template to the Additional Code section, as shown in the screenshot below:-config4.png

Once you have modified the code accordingly, update it and publish your changes.

I am also including a couple of test screenshots for your reference:-

1. Create an issue if you don't have an issue already created. Don't set any value for the Assets field as shown in the screenshot below:-

test1.png

 

2. Once the issue is created, the Assets field is displayed in the More Fields section, as shown in the screenshot below:-test2.png

 

3. Next, transition the issue to the Status in which Post-Function is added. The Post-Function has been added to the Start Progress transition in this example. test3.png

 

4. If there is any transition screen, set the values accordingly. The screenshot below includes only the Assignee and  Comment fields in the transition screen.test4.png

 

5. Once the issue has transitioned, the Asset field is updated accordingly and is moved to the Details section as shown in the screenshot below.  test5.png

Note: You may need to refresh your page once or twice to see the Asset field update take effect.

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
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events