Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Set field value with rest endpoint

I saw information about converting field and connect it to rest endpoint, but can I setFormValue in behaviors via rest endpoint? It returns only one value like: 

{"items":[{"value":3437754,"html":3437754,"label":3437754}]}

Script in behaviors:

getFieldByName("Price").setFormValue([
ajaxOptions: [
url: getBaseUrl() + "/rest/url",
formatResponse: "general",
]
])
And on create screen it set [Object object]

1 answer

0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Apr 22, 2022

I've encountered this.

I normally use formField.convertToSingelSelect([ajaxOptions:[...]])

But I found that I can't also set a value programmatically with that.

So, when i need to set the value dynamically, I use formField.convertToSingelSelect().setFieldOptions(map) instead.

Something like this:

import com.atlassian.jira.component.ComponentAccessor
import groovy.json.JsonParserType
import groovy.json.JsonSlurper
import com.atlassian.sal.api.net.Request
import com.atlassian.sal.api.net.TrustedRequest
import com.atlassian.sal.api.net.TrustedRequestFactory
import groovyx.net.http.URIBuilder
import com.onresolve.scriptrunner.runner.customisers.PluginModule

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
@PluginModule
TrustedRequestFactory trustedRequestFactory
def options = [:]
options.put("":"") //this is to have a blank option at the top

def url = "$baseUrl/rest/url"
//create a trusted request to the target url
def
request = trustedRequestFactory.createTrustedRequest(Request.MethodType.GET, url) as TrustedRequest
request.addTrustedTokenAuthentication(new URIBuilder(baseUrl).host, currentUser.username)

//prepare the parser
def
parser = new JsonSlurper().setType(JsonParserType.INDEX_OVERLAY)

//execute the request
def
response = request.execute()
def jsonResp = parser.parseText(response)
jsonResp.items.each {
options.put(it.html, it.html)
}
field.convertToSingleSelect().setFieldOptions(options)
field.setFormValue('someDefaultValue')

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events