Behaviour set a field

Sunjeev.Bhatia March 15, 2019

 

From a behaviour I couldn't access the value of a customfield

I finally managed to get access by

 

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue

def cField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("UserAuthFlag1")
def cFieldValue = underlyingIssue.getCustomFieldValue(cField).toString()

 

Now I want to update the same field and I am having issues - the code is saying to check that the declared type exists and if the method exists

err1.JPG

for code

def cField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("UserAuthFlag1")
def cFieldValue = underlyingIssue.getCustomFieldValue(cField).toString()

def cfConfig = cField.getRelevantConfig(underlyingIssue)


def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
    it.toString() == 'N'
}
 underlyingIssue.setCustomFieldValue(cField, "fred")

 

2 answers

0 votes
Robert Bromley July 26, 2023

Did anyone have a problem with setting select list fields?

0 votes
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.
March 16, 2019

HI @Sunjeev.Bhatia 

Hows things?

What kind of behaviour are you creating

getting field values in a from should be as simple as

def flag = getFieldById("UserAuthFlag1")

similarly setting a value

flag.setFormValue(value)

can you post a bit more context?

 

PS

You should sign up for the London Atlassian User Group. Lots of useful info to be gleaned from other users

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.
March 17, 2019

Hi @Sunjeev.Bhatia 

I had a quick play with this but I mustbe missing something as the API looks as if it should be straightforward

https://scriptrunner.adaptavist.com/latest/jira/behaviours-api-quickref.html

I've added other tags to post to attract the Adaptavist gurus

 

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueInputParameters

IssueService issueService = ComponentAccessor.getIssueService()
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()

def authenticationContext = ComponentAccessor.jiraAuthenticationContext
def cwdUser = authenticationContext.getLoggedInUser()

def cField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("UserAuthFlag1")
def cFieldValue = underlyingIssue.getCustomFieldValue(cField).toString()

// SKU CSV
// def skuField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("SKU CSV")
// def skuFieldValue = underlyingIssue.getCustomFieldValue(cField).toString()

log.info("cFieldValue: " + cFieldValue)
def flag = getFieldById("UserAuthFlag1")
log.info("flg: " + flag)

issueInputParameters.addCustomFieldValue(cField.id, "Y")
// issueInputParameters.addCustomFieldValue(skuField.id, "some text " + flag)
log.info("issueInputParameters: " + issueInputParameters.toString())
def update = issueService.validateUpdate(cwdUser, underlyingIssue.id, issueInputParameters)
log.info("update: " + update.toString())

if (update.isValid()) {
issueService.update(cwdUser, update)
}

 

 

 

Marc Minten _EVS_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 18, 2019

I think getFieldById"UserAuthFlag1") definitively does not work with the given argument. You should use getFieldByName(..) !

Sunjeev.Bhatia March 18, 2019

Hi Tom,

Doing well and you?

Still no joy on this

Code

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueInputParameters

def authenticationContext = ComponentAccessor.getJiraAuthenticationContext()

if (underlyingIssue != null)
{

    def reporter = underlyingIssue.reporter
    def reporter1 = underlyingIssue.getReporterId()
    log.warn("XXX1")

    IssueService issueService = ComponentAccessor.getIssueService()
    IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()

    def cwdUser = authenticationContext.getLoggedInUser()

    def cField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("UserAuthFlag1")
    def cFieldValue = underlyingIssue.getCustomFieldValue(cField).toString()





    log.warn("cFieldValue: " + cFieldValue)
    def flag = getFieldByName("UserAuthFlag1")
    log.warn("flg: " + flag)

    def CC = getFieldByName("Change Champion")
    def CCValue=CC.getValue()
    def au = getFieldByName("Authoriser User 1.")
      

    if (CCValue == reporter1)
    {
        au.setFormValue(null)
    }
    else
    {
       
        if (CCValue != au)
        {           
           issueInputParameters.addCustomFieldValue(cField.id, "N")
   
 
           log.warn("issueInputParameters: " + issueInputParameters.toString())
           def update = issueService.validateUpdate(cwdUser, underlyingIssue.id, issueInputParameters)
           log.warn("update: " + update.toString())

           if (update.isValid())
           {
              log.warn("update is valid")
              issueService.update(cwdUser, update)
          
           }
           else
           {
              log.warn("update is NOT valid")
           
           }
           au.setFormValue(CCValue)
           def cField1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("UserAuthFlag1")
           def cFieldValue1 = underlyingIssue.getCustomFieldValue(cField1).toString()
           log.warn("cFieldValue1: " + cFieldValue1)

       }
    }


}

 

 

Result set

 

 XXX1
 cFieldValue: Y
 flg: Form field ID: customfield_11201, value: null
 issueInputParameters: com.atlassian.jira.issue.IssueInputParametersImpl@796faf3c
 update: com.atlassian.jira.bc.issue.IssueService$UpdateValidationResult@26c2f482
 update is valid
 update: com.atlassian.jira.bc.issue.IssueService$UpdateValidationResult@51015060
 update is valid
 cFieldValue1: Y
 cFieldValue1: Y
 update: com.atlassian.jira.bc.issue.IssueService$UpdateValidationResult@7af33e12
 update is valid
 cFieldValue1: Y 

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.
March 18, 2019

Hi @Sunjeev.Bhatia 

Logging looks odd as it seems as some parts are run multiple times e.g. update is valid

I might be able to run this up on my server this evening to check

meantime I've commented your code with // ***

Just extra logging I'm curious about and some poss changes - unproven!

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueInputParameters

def authenticationContext = ComponentAccessor.getJiraAuthenticationContext()

if (underlyingIssue != null)
{
// *** which field is behaviour attached to?
def reporter = underlyingIssue.reporter
def reporter1 = underlyingIssue.getReporterId()
// *** logging
log.warn("XXX1" + reporter + " - " + reporter1)

IssueService issueService = ComponentAccessor.getIssueService()
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()

def cwdUser = authenticationContext.getLoggedInUser()

def cField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("UserAuthFlag1")
def cFieldValue = underlyingIssue.getCustomFieldValue(cField).toString()

log.warn("cFieldValue: " + cFieldValue)
def flag = getFieldByName("UserAuthFlag1")
log.warn("flg: " + flag)

def CC = getFieldByName("Change Champion")
def CCValue=CC.getValue()
def au = getFieldByName("Authoriser User 1.")
// *** check field "Authoriser User 1." vs "Authoriser User 1" one is a user field, one is a scripted field

// ***
log.warn("users: " + CCValue + " - " + reporter1)
if (CCValue == reporter1)
{
au.setFormValue(null)
}
else
{
// ***
log.warn("else: " + CCValue + " - " + au) // comparing value to a field object ??

if (CCValue != au)
{
// *** does flag.setFormValue("N") work?
issueInputParameters.addCustomFieldValue(cField.id, "N")

log.warn("issueInputParameters: " + issueInputParameters.toString())
def update = issueService.validateUpdate(cwdUser, underlyingIssue.id, issueInputParameters)
log.warn("update: " + update.toString())

if (update.isValid())
{
log.warn("update is valid")
issueService.update(cwdUser, update)
}
else
{
log.warn("update is NOT valid")
}
// *** this is performing same action in different ways - may clash, issue updates overwritten when form saves?
au.setFormValue(CCValue) // ** setting authorise user to change champion?
// *** def cField1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("UserAuthFlag1")
// *** def cFieldValue1 = underlyingIssue.getCustomFieldValue(cField1).toString()
// *** log.warn("cFieldValue1: " + cFieldValue1)
log.warn("au: " + au.getValue())
}
}


}
Sunjeev.Bhatia March 18, 2019

hi Tom,

 

o/p is

 

 XXX1sbhat5(sbhat5) - sbhat5
 cFieldValue: Y
 flg: Form field ID: customfield_11201, value: null
 users: CM1 - sbhat5
 else: CM1 - Form field ID: customfield_11207, value: CM1
 issueInputParameters: com.atlassian.jira.issue.IssueInputParametersImpl@2d9aca62
 update: com.atlassian.jira.bc.issue.IssueService$UpdateValidationResult@737a27ef
 update is valid
 au: CM1

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.
March 18, 2019

hi

the

else: CM1 - Form field ID: customfield_11207, value: CM1

tells me

if (CCValue != au) 
will always be true as you can't compare a string username to a user object
Sunjeev.Bhatia March 18, 2019

The field wasn't on the form - so I added it to the screen  - it can be updated easly using set field - its a pain cause the field is visible but I suppose I can live with it

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.
March 18, 2019

OK

behaviours endup as java script in the page so is dependent on the structure of the form

Sunjeev.Bhatia March 18, 2019

anyway to get round the CCValue and the au check?

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.
March 18, 2019

ok you'll need to get the user name

some logging might help, I haven't got a suitable field to hand on my jira. Not all of these suggestions are valid

log.warn("else: " + CCValue + " - " + au)

log.warn("else: " + CCValue + " - " + au.getValue())
log.warn("else: " + CCValue + " - " + au.value)
log.warn("else: " + CCValue + " - " + au.name)
log.warn("else: " + CCValue + " - " + au.key)
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.
March 19, 2019
if (CCValue != au.getValue())

does that work for you? 

Suggest an answer

Log in or Sign up to answer